Triggering export after table is redrawn to show all records
Triggering export after table is redrawn to show all records
Hello all,
I have datatables set up to use server side processing and as we all know, if you click export it will only export what it has in memory. This makes perfect sense, however I need the ability to export all rows. I know there are various other questions about this, and other ways to solve it, however I am interested in try to automatically change the page length to 'All' when the user clicks the export button.
i.e.:
"buttons":
[
{
extend: "edit",
editor: editor
},
{
extend: 'excel',
text: 'Export to Excel',
action: function ( e, dt, node, config ) {
theTable.page.len(-1).draw();
theTable.button('1').trigger();
}
}
]
The above throws a Stackoverflow (which I think makes some sense as it looks like an endless loop). Long story short how do I (or can I?) change the action of the excel export to first change the page length to 'All' then export the data? I can always instruct my users to manually select 'All' then export, but they won't listen.
I have also tried:
"buttons":
[
{
extend: "edit",
editor: editor
},
{
extend: 'excel',
text: 'Export to Excel',
action: function ( e, dt, node, config ) {
theTable.page.len(-1).draw();
$.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);
}
}
]
Which 'works' in that it does export the data but it doesn't wait for the draw to complete which means it only exports the first page. Is there anyway to have the $.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config);
wait until after the draw completes?
Thoughts?
This question has an accepted answers - jump to answer
Answers
Also on a side note when I try:
I get the following error:
Which is odd as it is almost exactly mirrors the example on https://datatables.net/reference/option/buttons.buttons.action
Perhaps I am missing something?
Try this:
The issue you are running into is that the
draw
action when using server-side processing is async. So you have to wait for the nextdraw
before the data is known to be loaded.Allan
Thanks Allan! However, when enter the above it results in the following error:
Just to confirm my setup is correct here is my code:
Allan,
I changed my code a little bit to include the non minified version of buttons.html5.js which gave me the ability to debug the issue I was getting. I found that when I comment out:
this.processing( true );
on line 989 andthat.processing( false );
on line 1202 and I was able to refresh (draw) the table showing all records and export the results as a excel doc.You don't need make that.processing( false );
Just call action button with $.fn.dataTable.ext.buttons.excelHtml5.action.call(this, e, dt, node, config); after dt.one scopes.
Allan,
I tried using the code you offered in your last comment, but as a result the export dialogue appears every time the table is drawn now. Is there any way to unset the on draw event so that it only fires for the export buttons?
Did you use
one()
rather thanon()
?Can you link to a page showing the issue so I can offer some help.
Thanks,
Allan
I've experienced the same issue same with @wblakenc regarding
this.processing is not a function.
I just want to add some additional detail to help future viewer of this thread.As Allan pointed out on this SO' Answer, this is a scoping issue.
Inside the dt.one draw scope,
this
should be the button object, which is not the case on the code above. so to fix this error, we should define this outside the draw scope, something like this:That should work now! Thanks Allan and everyone on this thread.
Hi Guys, the last example from @iscarface works great to export all the pages to excel on a server side scenario.
Have some questions
1) How can I revert back the page.len to some specific value or default once the excel was generated? On the top of my head I would chain a function to the excel export button to rest the page len to the default but have no clue on the name of that callback to code it.
2) Is there any parameter to tweak the colums headers style, color, etc of the exported excel file?
3) Any other fancy parameter to hide columns at export time?
Thanks in advance,
Gerardo
page.len()
Cheers,
Colin
Hi Colin, Thanks so much for your reply.
Your suggestions for the points 2 and 3 are a light at the end of the tunnel. Thanks so much for point me on the right path!
Regarding point 1 (The export all pages to Excel), I was mocking examples for two days with no success.
For this, I land on the following thread where the user vbalsamello expose a pretty handy way to manage the switching of the data.length to a big number, export the Excel avoiding screen redraw and reverting back all to the original state.
https://datatables.net/forums/discussion/comment/152343/#Comment_152343
In in my case I've used -1 for the data.length parameter that's what my server is checking to list all the records.
But the -1 never arrieve to the server and get lost in the roud.
If I use, the Select on screen that's configured with a All and -1 it works ok so the server part is working ok.
All the logic form vbalsamello looks correct but, I discovered that the preXhr function is receiving 'data' as string rather than object making impossible to add or change parameters like it is indicated in the manual at https://datatables.net/reference/event/preXhr.
So this cannot be done this way as you cannot operate with data that's a json in string format as if it were an object.
Researching again, I discovered a response from You suggesting to: a) JSON.parse() data, b) Add what you need c) JSON.stringify() to revert data to it's original state
https://datatables.net/forums/discussion/comment/132155/#Comment_132155
Well, before the long try an error with the preXhr, I performed the parse and stringify and all looks ok in the console.
Data shows length = -1 but when the code continue, at the 2nd preXhr data shows back again the original size and the -1 is lost.
The following is the code I'm using annd below this you can find the console output.
Console Output
I'm totally with you regarding to produce the Excel from the server, specially for a large datasets but, for now I need to release this with the excelHtml5 option and code all the server export part at a later phase.
Does you know the reason why data has changed to string inside preXhr?
Is there any way to change the properties of data inside preXhr but in a permanent way to do the Ajax call with those changes?
Sorry for the long explanation. Hope this also help others too.
cheers,
The second
preXhr
is happening on your next draw request and since you aren't changing the page length on the client-side in the first preXhr the second one is taking the value from the client-side.Isn't that the intention of that setup though? Make one request with the length at -1 and export that data. Then make another request to get things back to normal?
Thanks,
Allan
Hi Allan,
Can you please help me how to do that? means after export the data how to get back to normal?
Thanks & Regards,
Zia
As Allan said, length of
-1
means "everything" - so when you want to return to the standard page length, just send the page length as it was before,Colin
@GerardoV sorry for the late reply, I haven't check datatables forum for a while until I've seen this thread again.
Regarding your question:
Have some questions
1) How can I revert back the page.len to some specific value or default once the excel was generated? On the top of my head I would chain a function to the excel export button to rest the page len to the default but have no clue on the name of that callback to code it.
This is what I am doing it in my apps: