Webbly Brown Webbly Brown - 7 days ago 6
jQuery Question

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

Answer

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
            }
       },
       {
           extend: 'csv',
           exportOptions: {
                columns: [1,2,3,4] 
            }

       },
       {
           extend: 'excel',
           exportOptions: {
                columns: [1,2,3,4] 
            }
       }         
    ]