Export to CSV without showing table on the page
Export to CSV without showing table on the page
AngeloTheus
Posts: 4Questions: 1Answers: 0
Dear All,
Is it possible to do that? Is there any link for the sample code?
The issue is that I dont want to show huge data on the page and would like to press button and export to CSV directly.
Kindly Advise.
This discussion has been closed.
Answers
You could use a selectAll button.
https://datatables.net/reference/button/selectAll
Presumably the issue is not that you don't want to see any data. but that you don't want to have to see all of it in order to export.
Do you not want to see the DataTable at all? Put it in a
display:none
element and then usebuttons().container()
to get the Buttons element and attach it into the DOM somewhere visible so it can be used.Allan
Actually, I only need a button to export data to CSV. I dont need to see the data on the page. There are 200K rows and it took me very long to show the data on the page. Mostly, the sample coding always use table to show the data. after that, press "CSV" button to save in CSV file. This is OK if the data is small. what about my case that I need to export 200K rows?
Are you using server side processing?
Take a look at the FAQ titled "How can I export the whole table when using serverSide and Buttons"
https://datatables.net/faqs/index#Server-side-processing
Kevin
I have another question - why bother using DataTables at all? If you don't want to show the table or the data, you'd be far far better just having the server build the CSV file for you.
Allan
Yes, I'm using Server Side Processing. I've built PHP script to export to CSV file but it's very slow. That's why I'm looking for another alternative and find out that datatable jQuery provide a build in button to export to CSV.
The 2nd challenge is that, after I finish export to CSV by using datatables plugin, am I able to do another process, e. g write to log table? How to know that datatables plugin has successfully export to CSV?
The point is that the Buttons export is a client side tool and will only export the data contained on the page. With server side processing the only data contained on the page is the current page.
Unfortunately your PHP script or some other method is the only way to export data using server side processing.
Kevin
There is no way that Buttons' export is going to be faster than a script on the server-side, if the data is in a database, I'm afraid.
I'd suggest you profile your PHP script to see if it can be sped up.
Allan
Thank for all your comments and supports. I will double check again the PHP Script to speed up.