How can I pull the sorted data from the datatable?
How can I pull the sorted data from the datatable?
yatrix
Posts: 2Questions: 0Answers: 0
I am trying to create an export-to-csv for my project. How can I pull the data from the table in it's sorted state? aoData is still in the same state as when it was initially loaded. I can't use the flash export, for the record, which is why I'm writing my own.
Thanks in advance!
Thanks in advance!
This discussion has been closed.
Replies
@Yatrix: you just saved my day!
There is an API method call `_` which will get the data in sorted order: http://datatables.net/docs/DataTables/1.9.4/DataTable.html#_ . I've very strongly recommend using that instead.
DataTables 1.10 is going to include a new API which will make this much easier - the new call will be `rows().data()` - but until then, the underscore method can be used.
Allan
$table._('tr', {page: 'all'});
But I need all rows in the sorted order so that I can find which page my target row is in.
1. save the state of the request (i.e. sort order / filtering / pagination) in a session.
2. have your excel (or any other export) call your ajax with the saved query parameters and just use that result set. That way, you get just the raw dataset and can do with it what you will.
As an example, our users have a table displayed in front of them. They can select the sort ordering and filter out for a smaller dataset. Smaller could equal 5000+ records. On each request to the controller, I save the query parameters (sent by datatables object) and have a separate controller method called excel_export that uses THOSE parameters, saved off in a session var, to retrieve the data. The excel export view modifies that raw dataset and sends it back to the client. Very simple.
Allan