Link to home
Start Free TrialLog in
Avatar of condor888
condor888

asked on

How to implement server side pagination with Flask and DataTables

My application is written with the Flask framework. I also employed jQuery DataTables to draw tables. However, how can I do a server side pagination with Flask and jQuery DataTables? I found a cgi solution but doesn't seem to fit my application well. I'd like DataTables to use Flask HTTP APIs via Ajax.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

I do not know python, but for datatables you want to use ajax https://datatables.net/examples/ajax/index.html
Avatar of condor888
condor888

ASKER

Yes, I understand, but I'd like to do server side pagination with Ajax, any idea? Thanks!
The way it works is if your server side outputs 500 records, then that 500 gets sent to the client and the pagination is based on the 500 records. You can use the datatables api to create the increments of how many records to show at a time (10, 20, 50, 100, all etc)

If you have a very large amounts of data, then you will still use ajax, but set serverside processing to true.  http://datatables.net/release-datatables/examples/data_sources/server_side.html
With server-side processing enabled, all paging, searching, ordering actions that DataTables performs are handed off to a server where an SQL engine (or similar) can perform these actions on the large data set (after all, that's what the database engine is designed for!). As such, each draw of the table will result in a new Ajax request being made to get the required data.

Another option is to have a query that determines total amount of records (or cache that).  let's say you have 100,000 records and you only want to push 500 out at a time.  Then create server side generated pagination for the 200 pages.

I think the built in server side function for datatables is the way to go.
Hi Scott, I went to the doc page that you pointed to. But it seems that the PHP code is missing... Is there any code samples to demo how it works?

In my case, I have a huge dataset, so only part of the data should be returned each time. Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Scott, sorry about the late reply. I checked the code that you sent. It's rather a server-side processing than a server-side pagination. So I am afraid this is not a proper example for the pagination...
Actually, it is both.

Datatables will take in a specified number of records when you use server side processing/ajax. (See "Draw" in the api https://www.datatables.net/manual/server-side) and as needed will grab more data.

If you have 500 records and you tell datatables to takin all 500, then the pagination is built into datatables and his handled by the client. You can even select the number of rows to show at a time (10, 20, 50, all etc) and that creates your pages.

If you have 10,000 records and set 100 to come in at a time, datatables will automatically cache 100 at a time.