how to call multiple different ajax request when using datatables pagination.

how to call multiple different ajax request when using datatables pagination.

LemonSharkLemonShark Posts: 4Questions: 1Answers: 0
edited November 2023 in Free community support

I am not yet very familiar with datatables.
Is there any API or method that will call different api simultaneously after using pagination.
I am doing this because I have so many data from the server and it needs time to process.
Example.
After clicking the pagination
1st - It will request field=initVariables (This will get all the data for searching)
2nd - It will request field=polling (this will serve as a buffer )
3rd - if the polling has returned a success it will request field=logs (this will show the results )

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    Sounds really complicated. Not sure whether you can split it up like this. Would require a bit of custom code.

    I am doing this because I have so many data from the server and it needs time to process.

    If the problem is performance due to large amounts of data you could use serverSide. This will only load the data needed for the respective display, e.g. just 10 or 20 records depending on what your pagination settings are.

    https://datatables.net/reference/option/serverSide

    Another way could be deferRender. While this will load all the data from the server rendering will be deferred for those parts of the data table that are currently not visible.

    https://datatables.net/reference/option/deferRender

    I didn't notice much of a difference using deferRender, but I do use serverSide for some very large data tables. Works well.

  • LemonSharkLemonShark Posts: 4Questions: 1Answers: 0

    So it comes down to custom code. Thanks!!! I am already using server side.
    Its just that I need to call multiple ajax request because the api is split into 3 part(initVariables, polling, logs).

    But I will try coming up with the custom code. I already have one in mind I will customize in drawCallback hopefully it will work

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited November 2023

    The process you use will depend on how the API's work.

    You are using server side processing and I'm guessing the ajax option sends to the initVariables API. Does that API support the server side processing protocol and returning only the data for the page being displayed? Or are you augmenting the request with parameters specific to the API.

    How do you limit the polling and logs API requests to the records in the initVariables result?

    Can you create a server script that receives the server side processing request then calls the three APIs and packages the results into one response back to Datatables?

    You could use drawCallback and in the function use jQuery ajax() to fetch the polling and logs APIs. Assuming there is a unique ID to tie the data together you can loop through the Datatable rows, using rows().every() and update the rows from the be grabbing the corresponding polling and logs results.

    I'm just throwing out some general ideas as I don't know how the APIs work and how the data is to be combined. Can you provide more details of how you envision this working so we can offer more specific suggestions?

    Kevin

  • LemonSharkLemonShark Posts: 4Questions: 1Answers: 0

    '''The API works like this.
    1. field=initVariables - just send the the form data from the datatable
    2. field=polling - this will loop though until in successcallback the condition status = complete
    3. field=logs - this will display all the data gathered

    So the sequence is like
    field=initVariables - gets the form
    field=polling - status - incomplete so call again the ajax
    field=polling - status - incomplete so call again the ajax
    field=polling - status - complete call the field=logs
    field=logs - displays the data'''

    sorry if my explanation is not very good.

    I already tired the drawCallback and loop it using jQuery ajax.
    but the problems is that when I use the pagination. the pagination is page is not saved it always resets. because upon investigating the when I first called the
    - field=initVariables the datatable submits and refreshes the datatable but the field=initVariables call no data so the datatable gets empty
    - proceeds to call field=polling and when it gets a success ( I called sumbit data table)
    - so it calls fields=logs ( but does not retain the page)
    Note: I have a condition that changes the ajax.url in the datatable in the preDrawCallback.

    Q1. in my attempt above is there a way to not call/execute or to make it wait until the polling is done looping. Because after preDrawcallback the _fnDrawbackfire is called and gets executed. ?
    Or basically Is there a way in the predrawcallback to call the api initVariables and polling and it must finish first before calling _fnDrawbackfire?

    Q2. this is my another attempt. I have tried using preXhr but the problem is that preXhr does not wait until my polling is done. is there a way to make it pause or stop until polling is done? because after triggering the preXhr the predrawcallback gets called immediately and executes field=logs even though the polling is not yet finished looping.

    '''Can you create a server script that receives the server side processing request then calls the three APIs and packages the results into one response back to Datatables? '''

    in the suggestion above can you please explain more about this. Is there a method I can execute all the three api simultaneously in the datatables?

  • LemonSharkLemonShark Posts: 4Questions: 1Answers: 0
    edited November 2023

    What I mean by datatable submit is - datatable.api().ajax.reload

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Can you create a server script that receives the server side processing request then calls the three APIs and packages the results into one response back to Datatables?

    That is exactly what I would do. Rather than making a chain of three requests and three responses at the client-side, make one request and have the server-side script sort out the middle bits? Or are you implementing a buffer on the client-side?

    If you are going to provide your own logic in a chain line that, don't use the events - you need to use ajax as a function. It will tell you what data the DataTable wants, you can make whatever requests you need and then feed the result back to DataTables with the provided callback.

    Allan

Sign In or Register to comment.