Search code examples
jquerydatatabledatatablesangular-datatables

datatable : load data in chunks


Requirement :

I am looking to load a large dataset from server into the jQuery DataTables.

Instead of loading the entire data on one go, I would like to load the table chunk by chunk. Make an ajax call fetch few hundred rows when the user click on pagination index.

Example :

So, in a grid only 10 records per page will be visible and pagination will be upto 50 index(10(records per page)*50(pages) = 500(records)).on click of 50th index we want to fetch another 500 records and add in the same datatable and then index will be upto 100.

I have a dataset of 1500 records but at one time i am only able to fetch 500 records.

I am exploring various questions in SO and also in datatable forum but still no solution found so far.

Datatable Forum : Trying to do client side lazy loading


Solution

  • I'll give a conceptual answer since i don't know anything about your server. Basically on the client you have a page variable which initializes to 0. On your server you have a chunk variable which is set to 10. Each time you need to load data, you increment page and send an ajax request with the page as a parameter.

    Your server will then query the database for rows page * chunk through (page * chunk)+chunk-1). This requires that you select all rows, give each row an index, and then retrieve the proper chunk. Notice the -1 for [inclusive,exclusive] chunks.

    Example:

    page = 0; chunk start index = page * chunk = 0 chunk end index = page * chunk + chunk - 1 = 9

    Retrieve rows 0-9 from database.

    page = 1; chunk start index = page * chunk = 10 chunk end index = page * chunk + chunk - 1 = 19

    Retrieve rows 10-19 from database.

    page = 2; chunk start index = page * chunk = 20 chunk end index = page * chunk + chunk - 1 = 29

    Retrieve rows 20-29 from database.

    You can tweek the chunk variable to how ever many records you want per call.