Caching search request to serverside script

Caching search request to serverside script

doncullendoncullen Posts: 32Questions: 2Answers: 0

Due to working with over 40k rows, I've implemented server-side processing in conjunction with AJAX. Works pretty well. I also added column-searching, so the user could search using a specific column rather than applying global search. Once again, works pretty well.

I've been asked to make it possible to export the filtered results to an excel file. This is where I run into challenges. Buttons (as provided by DataTables) exports to excel the immediate view; useless if it's paginated and provides data via server-side processing. Doing some research indicates that the best solution is to have the server provide the Excel rather than Datatables.

After long and hard consideration, I've come to the conclusion I will have to use PHPExcel to be able to export the filtered results to an excel file made available to download. I'm not asking for help with PHPExcel (although if you have tips, those would be welcome!). My main issue is the search by column/global search filters. If searching is done via one of those, then the export must take those into account.

After some consideration, I've come up with a possible solution: cache the last request to the server-side script within a variable in Javascript. When the user hits the export to excel button, it'd execute the last cached request with one difference; it'd add an additional parameter to the request (exportexcel=1). Then on the server-side script, all I would have to do would be check $_REQUEST['exportexcel'] and see if it's set to 1, if so, then rather than outputting the data in JSON format, it'd pass it off to PHPExcel which would then output it in excel format.

So therein lies the question: how would I best tell Datatables to store a copy of it's request to the server-side script within a local Javascript variable (and trigger the request later with an additional parameter if a button with the class "exportexcel" was clicked)? If I was able to do it like this, then I'd be able to export data even with filtering applied.

I already know how to capture the button event, it's how to add an additional parameter to the server-side script request that has me at a loss, in addition to being able to cache the request for later re-triggering.

Thanks in advance for your assistance, time, and consideration!

This discussion has been closed.