saadaan's avatar

Using pagination with datatables

Hi,

I am using datatables to display a set of data as a table with options (download, search, etc). For the dataset being big, I have to use paginate() at controller level to divide the results. However, they do not make a good combination, because datatables applies its functions only to the data passed onto it (in my case, its the first 50 results). So, whatever functions datatables offers (download, search, etc) are limited to first page of paginated results by laravel, which is not OK.

Is there a way to make paginate work in coordination with datatables?

If not, then let me know how can I use the SEARCH and DOWNLOAD AS EXCEL function for table while using pagination.

thanks, Saad

0 likes
3 replies
MarianoMoreyra's avatar

Hi @saadaan

Depending on how big is your dataset, you don't need to paginate at controller level with DataTables.

You can simply read the dataset the first time the page loads following this example: https://datatables.net/examples/ajax/simple.html

And here you have the AJAX reference at the manual: https://datatables.net/manual/ajax

You should consider deferred rendering to have the best loading time: https://datatables.net/examples/ajax/defer_render.html

The other functions as search and export will still work really fast as always, as it will still be doing all the work at the front-end

saadaan's avatar

My dataset is huge, around 250k entries. I am using MySQL at the backend.

MarianoMoreyra's avatar

In that case you may go with the server-side option, although it requires a lot of work on the controller side in order to process the different interactions with datatable: https://datatables.net/manual/server-side

Also, you could just manage the search and the export by yourself. For the search, just adding a textbox and sending the value with AJAX (or simply a submit button) and then processing the search at your controller.

And for the export, you can use Laravel Excel library: https://laravel-excel.com/

I think at the end, this will require less effort, but it will depend on your needs.

Please or to participate in this conversation.