csvHtml5.action.call() results in JavaScript error: Uncaught TypeError: n is not a function
csvHtml5.action.call() results in JavaScript error: Uncaught TypeError: n is not a function

I am using server-side processing and would like to dynamically set the following config prior to exporting:
exportOptions: {
modifier: { search: 'applied', page: 'all' } // Ensure all even paginated rows are included in export.
}
After dynamically setting the config, I need to then dynamically export all rows upon clicking the CSV button.
I could not figure out how to do this so, as an acceptable alternative, I am dynamically changing the "Display Results" dropdown list via the following action export:
{
extend : 'csvHtml5',
action: function(e, dt, button, config) {
console.log("CSV export action triggered.");
// Check if server-side processing is enabled
if (dt.settings()[0].oFeatures.bServerSide) {
var originalPageLength = dt.page.len(); // Store original page length
// Change page length to `-1` to load all data
dt.page.len(-1).draw().one('draw', function() {
console.log("All rows loaded, triggering export.");
// Trigger the default CSV export action
$.fn.dataTable.ext.buttons.csvHtml5.action.call(this, e, dt, button, config);
// Restore previous page length after export
setTimeout(() => {
dt.page.len(originalPageLength).draw();
console.log("Page length restored to", originalPageLength);
}, 1000);
});
} else {
// If not server-side, trigger export normally
$.fn.dataTable.ext.buttons.csvHtml5.action.call(dt.button(button[0]), e, dt, button, config);
}
},
},
The problem with this approach, is that the following csvHtml5.action.call() reports the following JavaScript error:
Uncaught TypeError: n is not a function
Believe or not, the export still appears to work! However, because of the JavaScript error, the following call fails and does not reset to the previous page length after export.
$.fn.dataTable.ext.buttons.csvHtml5.action.call(this, e, dt, button, config);
I have spent too much time going round and round with an AI bot. I need a real person with real intelligence to take a look.
I am using the following CDN:
<!-- Preload and defer DataTables CDN and local configuration -->
<link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js" as="script">
<link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js" as="script">
<link rel="preload" href="https://cdn.datatables.net/v/zf/dt-2.2.2/b-3.2.2/b-colvis-3.2.2/b-html5-3.2.2/b-print-3.2.2/fh-4.0.1/sr-1.4.1/datatables.min.js" as="script">
<!-- DataTables CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js" defer></script>
<script src="https://cdn.datatables.net/v/zf/dt-2.2.2/b-3.2.2/b-colvis-3.2.2/b-html5-3.2.2/b-print-3.2.2/fh-4.0.1/sr-1.4.1/datatables.min.js" defer></script>
Answers
Datatables doesn't provide a way to dynamically change options unless there is a documented API like
page.len()
that can be used. With server side processing only the data displayed is at the client. Settingmodifier: { search: 'applied', page: 'all' }
won't force all the server side data to be exported.I believe you are missing the
cb
parameter, new in buttons 3, with this:See the
buttons.buttons.action
docs for details. I think it should look like this:The recommended method to export with server side processing is to use a server based export library. This way the full data set doesn't need to be downloaded to the client. See this FAQ for details and a link to a buttons plugin to send table state info to the server.
Kevin