title
is read once when the dataTable is initialised, and then the value is mapped into the internal config
object. Therefore, if you want to change settings dynamically, you must change that internal config
object, not try to change the readonly configuration settings.
So do it the other way around - create an event listener for the <select>
inside the buttons init()
callback itself. If you have a <select>
with optional filenames like this
<select id="filename">
<option value="filenameA">filename A</option>
<option value="filenameB">filename B</option>
<option value="filenameC">filename C</option>
</select>
Then you can dynamically change the export fileName
(== title
+ extension
) by
buttons : [
{
extend: 'excelHtml5',
title: 'filenameA', //default filename
init: function(dt, node, config) {
$("#filename").on('change', function() {
config.title = this.value;
})
}
},
You can change the other config properties from within the handler as well, for example you might want change config.extension
to something else.
Here is a demo -> https://jsfiddle.net/y8d9zhfv/
It is important to emphasize, that dataTables.buttons.js 1.3.0 or higher is required; this is also the case with the buttons.html5.js module. So if the above not work upgrade -> https://cdn.datatables.net/buttons/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…