Unclear on obtaining table data from a RESTfull API

Unclear on obtaining table data from a RESTfull API

rauls4rauls4 Posts: 10Questions: 0Answers: 0
edited March 2014 in General
My table works like a champ with a small dataset but now I need to access the real thing (100K+ records) using a provided RESTful API.

The API supports fetching data using this format:

serveraddress/api/locations?limit=100&offset=0

So my plan was to fetch results 100 at a time. The DataTables documentation says that in order to do this, I need to also send the site a bunch of other parameters (iDisplayLenght, iDisplayStart, iColumns, sEcho, etc. etc.)

Is that correct or can I just reload my table using an API call like: serveraddress/api/locations?limit=100&offset=100 instead?

If so, how do I detect when the table needs to load a new set?

If anyone knows of an example that deals with a simmilar situation it would be a huge help!

Thanks

Replies

  • remyjetteremyjette Posts: 7Questions: 1Answers: 0
    You can pass an fnServerData callback function to your DataTables initialization object, have it inspect the aoData argument to see what iDisplayLength, iDisplayStart, iColumns, etc etc are, and then make your own AJAX call to the API using the correct format for the API.

    However, unless the API allows you to provide parameters for filtering and sorting, you will be unable to perform those functions in your DataTable. In addition, you'll have to somehow workaround the lack of sEcho support.
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    @remyjette is spot on! Basically, you need to either explicitly use the DataTables server-side processing API or convert from and to the API DataTables expects. It does have a number of requirements, as remyjette says.

    Regarding sEcho - it isn't a mediatory parameter, you can do without it - but it can be useful for when the server has a heavy load on it, since it prevents data requests being drawn out of sequence.

    There is one more option - rather than using server-side processing, you could use client-side processing and load the data in chucks. i.e. set up a loop that makes a request every 500mS for 1000 more records and use the DataTables API to add the new data to the table. I'd generally recommend against this as server-side processing is hugely more efficient, but if you are constrained by the REST API, it might be the only way.

    Regards,
    Allan
This discussion has been closed.