dynamically change filename when exporting

dynamically change filename when exporting

protomeprotome Posts: 25Questions: 11Answers: 1
edited January 2016 in Free community support

hello, i am using buttons to export my table to files. I understand I can change the name by setting "title".
Is there a way to change it dynamically, so everytime the user clicks on the button, it gets a new name ? like name+timestamp to differentiate them .
I put that in the title
thanks, pat

Replies

  • protomeprotome Posts: 25Questions: 11Answers: 1

    oups, my fault. I was trying to export same table without changing the data, of course the name stayed the same. Once I change the data, name changes as I say.

    I post my answer in case somebody else does the same mistake! pat

  • TronikTronik Posts: 122Questions: 28Answers: 1

    Hi!
    I have this problem and Im curious on how you solved it.
    I want to get values from two input fields, but I only get the values which were loaded on page init.

    "buttons": [
                 {
                extend: 'colvis',
                text: 'Show/hide columns'
                },
                {
                    extend: 'excelHtml5',
                    title: 'Sold_Products_'+$("#date").val()+'-'+$("#date2").val(),
                    footer: true
                },
            ],
    
  • allanallan Posts: 62,857Questions: 1Answers: 10,344 Site admin

    Each of the export button types has a filename option which can be used to set the export file name. If you need it to be updated dynamically, that option can be given as a function that will be executed at the time of export.

    Allan

  • TronikTronik Posts: 122Questions: 28Answers: 1

    Hi Allan,

    Thank you, how would I implement this in my above code?

  • allanallan Posts: 62,857Questions: 1Answers: 10,344 Site admin

    Use the filename option :smile:. See the excelHtml5 documentation for more information about that option.

    Allan

  • yonatanyonatan Posts: 5Questions: 1Answers: 0

    I tried to use the filename option - but it was called only once, when the document was loaded, and not when the button was pressed. Is this a bug or am I doing something wrong? By the way I couldn't see anywhere in the documentation that says it can be given as a function.
    ...
    new $.fn.dataTable.Buttons( logTable, {
    buttons: [
    {
    extend: 'csv',
    text: 'Save Log Info',
    filename: getExportFileName()
    }
    ]
    } );
    ...

  • wyattbikerwyattbiker Posts: 25Questions: 14Answers: 0

    works for me

    extend: 'csv',
    text : 'Export to CSV',
    filename: function(){
                    var d = new Date();
                    var n = d.getTime();
                    return 'myfile' + n;
                },
    
  • yonatanyonatan Posts: 5Questions: 1Answers: 0

    Hmm - Thanks

    I changed my example to

                    {
                        extend: 'csv',
                        text:   'Save Log Info',
                        filename: function () { return getExportFileName();}
                    }
    

    which indeed works, so it looks like the implementation is that there are three options
    1) string - File name to give the created file
    2) expression, (including a function call) - call that function to get the filename when the definition is made
    3) definition of an anonymous function - execute the function when the button is pressed to get thr name of the file

    Did I miss the documentation on this somewhere ?

  • allanallan Posts: 62,857Questions: 1Answers: 10,344 Site admin

    The csv documentation should state that it can be a string or a function. However that appears to be missing the "type" information at the moment. I'll look into that.

    Allan

This discussion has been closed.