Is it possible to change the filename used for export of an already initialized table
Is it possible to change the filename used for export of an already initialized table
Hello,
I have a table where the user can load data depeding on his selction. I use Ajax to reload the data in the table.
I am also using buttons to export the content to an Excel file. I would like to change the name of the file based on the data loaded.
This is what I have in the table creation part (it's only the part where I create the Excel button):
var table = $('#access_data').DataTable( {
buttons: [ {
extend: 'excelHtml5',
filename: $('#year').val() + '-' + $('#month').val() + ' - Data comparaison.xlsx'
}]
});
Because the selection has not been made by the user when the page is loaded and also because the user might change the selection at any time, $('#year').val()
and $('#month').val()
don't have the correct value at intialization.
Is it possible to change this options after table initalization?
I tried this, but it doesn't work:
$('#access_data').on('xhr.dt', function (e, settings) {;
table.buttons([{
extend: 'excelHtml5',
filename: $('#year').val() + '-' + $('#month').val() + ' - Data comparaison.xlsx'
}]);
});
Thanks!
This question has accepted answers - jump to:
Answers
Yes, you can use
filename
as a function. As a function it will be executed at the time when the button is activated.Make sure you are using the latest version of Buttons . I can't remember exactly which version that feature was in - it might have been 1.4.0.
Allan
Thanks! Could you give me an example? because I can't seem to make it work.
I'm using version 1.4.0 (which seems like the latest)
Ok I figured it out... this is what I have now:
THANKS!!
Awesome. Just used this for "title" (pdf, xlsx) and for "filename" (pdf, xlsx, csv). Thanks a lot!
"cfFilename" and "cfTitle" are set based on the selected item's attributes.