Search code examples
jquerydatatables

Ignore first column in datatables CSV export


I'm trying to export a CSV file based on data from jQuery datatables and I would like it to exclude the first column of data, here is my current script.

 <script>
 $(document).ready(function() {
 $('#example').DataTable( {
    dom: 'Bfrtip',
    lengthMenu: [
        [ 10, 25, 50, -1 ],
        [ '10 rows', '25 rows', '50 rows', 'Show all' ]
    ],
     buttons: [
        'copy', 'csv', 'excel', 'pdf', 'print', 'pageLength'
    ]
} );
} );
</script>

What do I need to add to get that to work, please


Solution

  • You need to use the exportOptions within your buttons definition.

    buttons: [
           {
               extend: 'pdf',           
               exportOptions: {
                    columns: [1,2,3,4] // indexes of the columns that should be printed,
                }                      // Exclude indexes that you don't want to print.
           },
           {
               extend: 'csv',
               exportOptions: {
                    columns: [1,2,3,4] 
                }
    
           },
           {
               extend: 'excel',
               exportOptions: {
                    columns: [1,2,3,4] 
                }
           }         
        ]