I am using new feature in Datatables: "HTML5 export buttons". I am loading data with Ajax.
https://datatables.net/extensions/buttons/examples/html5/simple.html
The problem is that it only export the page that is currently displayed.
I am exporting like this:
buttons: [
{
extend: 'pdfHtml5',
text: 'PDF',
exportOptions: {
"columns": ':visible',
}
},
]
How can I export all rows?
You need to tell the AJAX function to get all data, then do the export but cancel the actual draw so that all of that data isn't loading into the DOM. The full data will still exist in memory for the DataTables API though, so you need to refresh it to the way it was before the export.
and:
This button definition worked for me in a scrolled table (instead of paging):
It will force the DataTable to request all rows for the current filtering for one request. Then it calls the desired action of the Export button directly. The variable
addOptions
can be used to alter the standard configuration of the export button.You might run into problems though if you have a lot of rows as they are all loaded into the DOM.
I know this is an old question, however for anyone struggling with this, here's my solution.
Variables:
Download button definition:
Ajax definition:
'preDrawCallback' function:
Server-side:
if(download == false), then server executes SELECT columns FROM tables WHERE rowNumber BETWEEN firstRow AND lastRow and outputs result for normal display within DataTable.
if(download == true), then server executes SELECT columns FROM tables and stores all rows formatted as CSV file (or any other file format depending on what Your server environment is capable to produce) server-side for later retrieval by GET request.
Following is ASP JScript code that I've used server-side:
The answer of Selcuk will work absolutely fine if we can fix the value of "All" beforehand. Suppose the number of rows is stored in variable row_count. Then
I'm using Datatables Version: 1.10.15, and got @kevenpo's answer to work. I had to modify it a bit to handle our server-side parameters, but that was the only stumbling block. I changed his line:
data.length = 2147483647;
todata.params[2]= -1;
because we stored our server-side parameters in a params sub-array. I have not tested it yet with a very large dataset to see what the performance is, but this is a very clever solution.According to DataTables documentation there is no way to export all rows when you are using server side:
I worked this around by adding an 'ALL' parameter to the length menu and training end users to display all records before doing a PDF (or XLS) export: