Populate table using several / multiple ajax requests

Populate table using several / multiple ajax requests

frasenfrasen Posts: 4Questions: 1Answers: 0
edited September 2015 in Free community support

Is it possible to populate a datatable by making several asyc ajax requests? For example, lets say the following URLs return JSON:

/ourserver/banana

/ourserver/apples

/ouserver/pears

Is it possible to start 3 ajax requests, one for each url, so that when each finishes the datatable gets updated with the results.

This would be good, for example, if the response times of the URL's differ a lot, so that the user sees the results of the requests which complete first, first.

Or if it's simply not possible to join the result of the 3 URLs so that they have to be called seperately. Is this possible via datatables?

Replies

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    It can be done.

    You need to set variables that indicate the completion of each Ajax call. Start with false and then as each Ajax call completes, set each one to true. The results of each Ajax call needs to be pushed into an object (or array) that you eventually hand over to Datatables. Make sure this is scoped globally so all the functions can get to it.

    Use the javascript setInterval() method to setup a recurring call. This call would check the status of the 3 status variables. If they are all true, you then invoke DataTables - and cancel the setInterval() with clearInterval(). This should be setup before you make the ajax calls.

  • allanallan Posts: 63,745Questions: 1Answers: 10,509 Site admin

    You might also want to consider using rows.add() which will let you dynamically add rows to a table. So you set up your table and then fire of the three Ajax requests and just have the data from each added to the data whenever they return.

    Allan

This discussion has been closed.