Configuring custom datatables export excelHtml5 file name with selected text

I am wondering how to set a custom file name for export to excelHtml5 datatables buttons using select, I executed a function to pass the name, but I am not setting its bad post for my js code. with a warning, it reflects the changes, but when I call the excel button in the datatables, it becomes empty.

Here is the code:

var reportName = '24 afterhours ';
$('#example').DataTable({
   dom: 'Bfrtip',
   buttons: [
      {
         extend: 'excelHtml5',
         title: reportName
      },
      {
         extend: 'pdfHtml5',
         title: 'Data export'
      }
  ]
});

$('#campaing').change(function() {
   reportName += $(this).find(":selected").text() + ' report';
});

I think I missed something.

+3
source share
1 answer

title , dataTable, config. , , config, .

, - <select> init(). <select> ,

<select id="filename">
    <option value="filenameA">filename A</option>
    <option value="filenameB">filename B</option>
    <option value="filenameC">filename C</option>
</select>

fileName (== title + extension)

buttons : [
   {
    extend: 'excelHtml5',
    title: 'filenameA', //default filename
    init: function(dt, node, config) {
        $("#filename").on('change', function() {
            config.title = this.value;
        })
    }
},

, , config.extension .


https://jsfiddle.net/y8d9zhfv/ , dataTables.buttons.js 1.3.0 ; buttons.html5.js. → https://cdn.datatables.net/buttons/

+4

Source: https://habr.com/ru/post/1623637/


All Articles