user3231742
user3231742

Reputation: 77

Dynamically changing the file name of jquery Datatables exported excel using buttons api

From Api:

$('#myTable').DataTable( {
    buttons: {
        buttons: [
            {
                text: 'Alert',
                action: function ( e, dt, node, config ) {
                    config.title ="dynamic title"
                }
            }
        ]
    }
} );

This is changing the title, but export is not working now. Any suggestion or workaround will help.

Upvotes: 3

Views: 4171

Answers (2)

lordvcs
lordvcs

Reputation: 2942

This should work, please note, here filename is a function reference and not a function call

$('#myTable').DataTable( {
    buttons: {
        buttons: [
            {
                text: 'Alert',
                filename: () => `dynamic filename ${new Date()}`
            }
        ]
    }
} );

Upvotes: 0

davidkonrad
davidkonrad

Reputation: 85528

See https://datatables.net/forums/discussion/33209/run-script-before-exporting-begins-buttons-plugin You need to call the original action programmatically. Small example :

$('#example').DataTable( {
   dom: 'Bfrtip',
   buttons: [{
      extend: 'excel',
      action: function(e, dt, button, config) {
        config.filename = dynamicVariable;
        $.fn.dataTable.ext.buttons.excelHtml5.action(e, dt, button, config);
      }
    }]   
})   

var dynamicVariable = 'qwerty';

Will produce a qwerty.xslx see -> https://jsfiddle.net/2ez9mxop/2

Upvotes: 3

Related Questions