raviraja
raviraja

Reputation: 706

Prompt the user for filename when doing Data tables export to excel

I am trying to export the jQuery data tables to excel,in that i want to ask the user for the filename when Export button is clicked and download the file once filename is entered.

$('#myTable').DataTable({
    buttons: [
        {
            extend: 'excel',
            "text":'Excel <span class="glyphicon glyphicon-download-alt"></span>',
            className: 'btn btn-success',
            filename: 'fixed_file_name'
        }   
    ]
});

Currently if i add function for getting filename,first the file is downloaded and then the function is getting executed.like below

filename: function(){
return prompt('Please enter file name');
}

I want to call a function which prompts the user for entering the filename and once the user enters the filename it should be assigned to filename data attribute. Any help would be appreciated.

Upvotes: 3

Views: 772

Answers (1)

yash
yash

Reputation: 2271

To dynamically chnage filename, refer this, which you are already referring.

But inside function, you want to prompt to enter something, and want to return that . then you can use something like below :

filename: function(){
    var filename = prompt("Please enter your file name", "");
    if (filename != null) {
        return filename;
    }
}

Edit:

looks like you had some other issue with your datatable. I also implement the same, and above solution works for me fine.

See Working Demo

Hope this will work for you.

Upvotes: 1

Related Questions