How can i export only specified columns in csv?

How can i export only specified columns in csv?

bhavesh123bhavesh123 Posts: 1Questions: 1Answers: 0
edited November 2020 in Free community support

Hi, i have implemented the datatable code and have displayed four buttons copy,csv,excel and pdf.
I am displaying 6 columns on the page, but when user clicks any of the button only column 1-4 should get exported or printed.
Now here is my code,

 $('.js-exportable').DataTable(
{
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf',{
          // messageTop: 'The information in this table is copyright to Sirius Cybernetics Corp.',

           extend: 'print',
           footer: true,
           title:'Monthly Attendsance Report <br><br><font size="3">Sales person : '+dept_name+'<br></font><font size="2">All Punches Report for  '+edate+'</font>',
            customize: function ( win ) {
                        $(win.document.body)
                            .css( 'font-size', '5pt' )
                            /*.prepend(
                                '<img src="http://datatables.net/media/images/logo-fade.png" style="position:absolute; top:0; left:0;" />'
                            );*/

                        $(win.document.body).find( 'table' )
                            .addClass( 'compact' )
                            .css( 'font-size', '11pt' );
                    },
           exportOptions: {
                columns: [1,2,3,4]
            }
             }
              
        ]
   });

On clicking pdf i can see only 4 columns, but on clicking csv all the visible columns are being exported.
And also i cant see the excel button.
Thanks

Answers

  • kthorngrenkthorngren Posts: 21,132Questions: 26Answers: 4,918

    It looks like these buttons should export all columns:
    'copy', 'csv', 'excel', 'pdf'

    And the Print button should export columns: [1,2,3,4].

    Is this not the case? Sounds like the PDF button is exporting the four columns.

    If you want to export certain columns fo reach button then you need to use exportOptions for each button like this example.

    And also i cant see the excel button.

    Likely you haven't loaded all of the required libraries. See the requirements here. You can use the Download Builder to get the required files.

    If you still need help please provide a link to your page or a running test case showing the issues.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.