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

davengdaveng Posts: 9Questions: 2Answers: 0

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

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    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

  • davengdaveng Posts: 9Questions: 2Answers: 0
    edited August 2017

    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)

  • davengdaveng Posts: 9Questions: 2Answers: 0
    edited August 2017

    Ok I figured it out... this is what I have now:

    var table = $('#access_data').DataTable( {
       buttons: [ {
          extend: 'excelHtml5',
          filename: function () {
             return $('#year').val() + '-' + $('#month').val() + ' - Data comparaison';
          }
       }]
    });
    

    THANKS!!

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    edited August 2017 Answer ✓

    Awesome. Just used this for "title" (pdf, xlsx) and for "filename" (pdf, xlsx, csv). Thanks a lot!

     buttons: [
                {extend: "edit", editor: cashFlowEditor},
                {extend: "editDates", editor: cashFlowDatesEditor},
                {extend: "pdfCashflow", 
                    title:    function () { return cfTitle; },
                    filename: function () { return cfFilename; }
                },
                {extend: "excelCashFlow",
                    title:    function () { return cfTitle; },
                    filename: function () { return cfFilename; }
                },
                {extend: "csvCashFlow", 
                    filename: function () { return cfFilename; }
                },
                         "colvis"                 
            ],
    

    "cfFilename" and "cfTitle" are set based on the selected item's attributes.

This discussion has been closed.