CSV export with Server-Side
CSV export with Server-Side
Hi, I have dataTables table with several customised filters and server-side processing. Everything works fine. Now I need to export csv (for a start). I added
dom: 'Bfrtip',
"buttons": [
{
text: 'Export CSV',
action: function (e, dt, node, config) {
$.ajax({
"url": "scripts/server_processing.php",
"data": dt.ajax.params(),
"success": function(res, status, xhr) {
var csvData = new Blob([res], {type: 'text/csv;charset=utf-8;'});
var csvURL = window.URL.createObjectURL(csvData);
var tempLink = document.createElement('a');
tempLink.href = csvURL;
tempLink.setAttribute('download', 'export.csv');
tempLink.click();
}
});
}
},
The button 'Export CSV' is visible and the export is working, but only on the visible data on one page. The data are filtered according to selected filter, which is great. So I think the only problem is with pagination. I found several solutions. Many of them manages it by selecting "All" entries and then export, which is no the right way, because I will need to export thousands of rows. Definitely, I need to use a server-side process to create the files to download.
Similar question is here
https://datatables.net/forums/discussion/39680/is-there-a-way-to-export-all-while-using-server-side-processing/p1
but it also exports the visible data only when I implemented it.
I have few questions:
To export CSV, do I need special script where will be the data send or I can use the customised server_processing.php which is downloadable from datatables.net website? Is it the same with the export to EXCEL and PDF ?
Is there some easy way how to include all pages to export?
Thank you
Answers
Please see this FAQ regarding the export buttons and server side processing. The recommended option is to use server based tools for exporting the data to alleviate the need to send all the data to the client.
Kevin
Hi Kevin, thank you, I mentioned it in the question, that I need server-side tools to export data. I just figured it out with modifying the ssp.class.php script where I changed the $limit to export more rows than visible.
Now I'm going to make export to pdf and xls, hope it will go as easy as csv
Thanks
Oh, sorry. Mis-read the question I thought you were still wanting to use the Datatables export tools.
Kevin
@culter, great to hear that you managed to find a way to get the server-side export working for csv files. Are you able to share your script of ssp.class.php to understand how achieved that and so that others can adapt in their project. Thanks
I have a .net core Generic DataTable Wrapper and i've managed the server side export with a simple taks.
Programatically I've set the page to 0 and the length to a big number, attach a callback to draw and in this call back I execute the button funcition. Remove this callback and reload the data table to the page and length that is setted before the user clicks on button.
this is my buttons element:
And this is serverSideButtonAction
its not working in Chrome anymore
-- I think its about Timeout number vs load speed....
@nexeon We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin