export csv file for selected rows from datatable

export csv file for selected rows from datatable

bhowmickbhowmick Posts: 3Questions: 2Answers: 0

I think many of them already asked this question but I didn't find answer that fits with my problem.I am new in Jquery so I copy paste few datatable examples to make workable for my problem.I have created datatabale using following examples: 1.Individual column searching https://datatables.net/examples/api/multi_filter.html 2.File export https://datatables.net/extensions/buttons/examples/initialisation/export.html
And my following code is:

$(document).ready(function() { // Setup - add a text input to each footer cell $('#example tfoot th').each( function () { var title = $(this).text(); $(this).html( '' ); } ); // DataTable var table = $('#example').DataTable({'scrollX':true, 'dom': 'lBfrtip','buttons': ['csv']}); // Apply the search table.columns().every( function () { var that = this; $( 'input', this.footer() ).on( 'keyup change', function () { if ( that.search() !== this.value ) { that .search( this.value ) .draw(); } } ); } ); } );

This code is working perfectly. Now I want export only selected rows without changing datatable structure in example 1. So anyone can help me please?
I have tried with this modification code also

var table = $('#example').DataTable({ 'ScrollX':true,'sDom': 'lBfrtip','sTableTools': { 'sRowSelect': 'multi','sButtons': [{'sExtends': 'csv','bSelectedOnly': true}]}});

But didn't workout for me!!
Thanks

Answers

  • bhowmickbhowmick Posts: 3Questions: 2Answers: 0

    I have managed to solve that problem
    '''

    $(document).ready(function() { // Setup - add a text input to each footer cell $('#example tfoot th').each( function () { var title = $(this).text(); $(this).html( '' ); } ); // DataTable var table = $('#example').DataTable({'scrollX':true, 'dom': 'lBfrtip',buttons: [{ extend: 'csv',text: 'CSV all'},{extend: 'csv',text: 'CSV selected',exportOptions: {modifier: {selected: true}}}],select: true}); // Apply the search table.columns().every( function () { var that = this; $( 'input', this.footer() ).on( 'keyup change', function () { if ( that.search() !== this.value ) { that .search( this.value ) .draw(); } } ); } ); } );

    '''

This discussion has been closed.