Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change export file name dynamic in data Tables?

 $("#dataTable").DataTable({                    
                    dom: 'Bfrtip',
                    buttons: [
                             { extend: 'excel', text:'export to excel',title:'1'},
                    ],
})

I can change the text of the button by the following code,but I can't get the title property.

var table= $("#dataTable").DataTable();
tabele.button(0).text('excel');
like image 892
Yin X. Avatar asked Jan 10 '16 07:01

Yin X.


2 Answers

once it has been set in the object and the datatable initialized it can't be changed. you can set it dynamically from a page element on the init though.

("#dataTable").each( function(index) {
    var exportTitle = $("#somePageElement").text();
    $(this).DataTable({
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'excel',
                title: exportTitle 
            },
            {
                extend: 'pdf',
                title: exportTitle 
            }
        ]
    });

this post has a good suggestion on how to deal with this as well. Setting up a custom file name datatables export excelHtml5 with a select text

like image 146
Ryan Vettese Avatar answered Nov 14 '22 14:11

Ryan Vettese


In my case, I had to override the button's action, to add something to change the title, then call the original button method.

Extending CSV in this example:

 $("#dataTable").DataTable({                    
    dom: 'Bfrtip',
    buttons: [
        {extend: 'csv', 
        text:'export to csv',
        title:'1',
        action: function(e, dt, button, config) {
        config.title = "New title";

        // This checks whether the HTML5 or flash version needs
        // to be called, so it's not needed if the button you are using 
        // doesn't have that distinction
        if ($.fn.dataTable.ext.buttons.csvHtml5.available( dt, config )) {
            $.fn.dataTable.ext.buttons.csvHtml5.action(e, dt, button, config);
        }
        else {
            $.fn.dataTable.ext.buttons.csvFlash.action(e, dt, button, config);
        }
    }}
    ]
})
like image 43
Fodder Avatar answered Nov 14 '22 15:11

Fodder