Ajax json request with limit and skip because of large data

Ajax json request with limit and skip because of large data

msalvamsalva Posts: 4Questions: 1Answers: 0

Hello friends,

I am relatively new to datatables api. But I really love it.

My datatable is requesting json with ajax. The problem I have is, that it would take way too long to load whole json from my server.
On server-side, I implemented 'limit' and 'skip' query parameter to limit the json response.

Is there a way to adapt this to datatables?

My default ajax url in datatables is '[domain]/testjson/trackings.json'.

For example, user has selected to show 10 entries per page and is on the first page, then ajax request url should be '[domain]/testjson/trackings.json?limit=10&skip=0' instead of my default url.

If the user goes to the second page, then ajax should be '[domain]/testjson/trackings.json?limit=10&skip=10' to load the second 10 results from server.

Is there a build-in function to solve this? Or how should I handle this?
Please let me know if I should phrase my question differently or if it is clear to understand.

Thank you in advance!

Best regards,
Matteo

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,445Questions: 26Answers: 4,974
    Answer ✓

    Datatables uses Server Side Processing for server side paging. See the Server side processing protocol for details. Datatables provides some server side libraries. See this blog for their usage.

    Kevin

  • msalvamsalva Posts: 4Questions: 1Answers: 0

    Hello @kthorngren

    Thanks a lot for your quick response which seems to answer my question.

    It would be helpful if you could confirm if I understood correctly the following:
    I need to add the following code to my Datatable:

    $('#example').DataTable( {
        serverSide: true,
        ajax: '/data-source'
    } );
    
    • then I would need to change the query parameter names on my server from 'limit' to 'length' and from 'skip' to 'start'

    Is that correct?

    Thank you and best regards,
    Matteo

  • kthorngrenkthorngren Posts: 21,445Questions: 26Answers: 4,974

    That sounds correct. Did you try it?

    Kevin

  • msalvamsalva Posts: 4Questions: 1Answers: 0

    @kthorngren if this works it would be awesome!!
    I will try soon and update.

    Thanks again!
    Matteo

  • kthorngrenkthorngren Posts: 21,445Questions: 26Answers: 4,974

    Just make sure your server script returns the expected response data so datatables knows the number of records, etc.

    Kevin

  • msalvamsalva Posts: 4Questions: 1Answers: 0

    @kthorngren thank you for the hint, I will make sure to adjust the response accordingly.

    Matteo

Sign In or Register to comment.