Export data in all child tables as a ZIP file from the parent table.
Export data in all child tables as a ZIP file from the parent table.
Description of problem:
In the parent table (see below):
Clicking on the Table ID, will open a child table (with more detail content) in the bottom of the page.
There is a usecase that we wish to download (export) all child tables' content from this parent table. (To prevent tedious clicking one after the other.)
Currently, only the cell data is exported from the parent table using the export function, i.e. just Table ID. I wonder if it is possible to support the fetching of child tables' content and export them as a ZIP file from in client side?
As we already know the list of table IDs, while clicking on the export button in the parent table, we can call the endpoint to fetch the whole data of all tables, and making saving to Excel/CSV, then zip them together as one ZIP file.
The part I mentioned above requires a customized process, and I think it's feasible to implment this myself (since it seems not possible to be part of DT?).
But is there a way to skip the initialization of the child tables and use the export function directly in the parent table? The current export function seems to bind the initialized table.
Replies
It will be very difficult to do this client side. Rather impossible I am tempted to say.
I do this server side using PHP Spreadsheet. This also has the advantage that you can use XLSX-templates for example that look much nicer than the client side Excel export function of Data Tables. You can also combine data table data sent to the server with additional data that you read from the server etc.
You can use this to export all table data and then send them to the server using ajax, for example.
https://datatables.net/reference/api/buttons.exportData()
It is absolutely confusing that this is called "buttons.exportData()". A button is not required for this. It is merely part of the code of the buttons extension that you will have installed anyway. This misunderstanding prevented me from using this for a couple of years ...
Plus something from my own coding: Ajax call to send table data to the server:
Making a zip file with PHP is pretty easy. I think it was built into PHP until version 8.1
https://www.php.net/manual/en/zip.setup.php
@rf1234 Thanks for sharing your experiences with this!
I am also considering doing this on the server side. If a lot of child tables are expected to be exported, this definitely has to be done on the server side concerning the time.
In my case, since my backend is Python and runs on a limited resource with only 4 CPU cores, I tried to reduce the load on the endpoints, i.e. use client side for excel/csv/zipping.
By skipping the initialization of child table, I guess it would be very fast to export.
Apparently, this function is considered to be a plug-in rather than a core export function. (Unless many people want this?)
I'll see if I could come up with something usefull and post back with some codes.
These two threads might give you some ideas of how to add data and new Excel sheets to the Excel export functionality:
https://datatables.net/forums/discussion/58960/how-to-add-rows-to-excel-export-or-why-are-my-child-rows-not-exported#latest
https://datatables.net/forums/discussion/58998/excel-export-create-new-sheet-example#latest
Kevin
Interesting! Good luck! Maybe you'll find something similar for Python as well?
I use generated zip files mainly to bundle user-uploaded files and make it easier for them to download their stuff in case they uploaded a lot.
I also us zip-archives to allow the download of all documents of an entire client installation (well, not a real but a virtual installation). Even that works like a charm, even though the generated zip archives can be very large.