Refresh rows without ajax call

Refresh rows without ajax call

nboisteaultnboisteault Posts: 4Questions: 1Answers: 0

Description of problem:
Hi,
I have a datatable populated by ajax calls.
I use the createdRow callback to add a class on some rows based on some local sates.
When a state changes I call the draw() method but I see that it sends an ajax request I don't need.
Is it possible to execute the createdRow callback or refresh the table without sending an ajax request?
Maybe the draw() method could take a parameter to refresh the table with current data?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,079Questions: 89Answers: 427

    Just deactivate "serverSide" and the problem will be gone.
    https://datatables.net/reference/option/serverSide

    With "serverSide" activated an ajax call is required when redrawing the table because Editor may not have all the required rows downloaded from the server yet.

  • kthorngrenkthorngren Posts: 21,838Questions: 26Answers: 5,048

    When a state changes I call the draw() method but I see that it sends an ajax request I don't need.

    Are you saying you are calling draw() in createdRow?

    Please post your Datatables code so we can see what you are doing. If you are calling draw() in -=option createdRow` we might be able to offer suggestions to eliminate the call.

    Kevin

  • nboisteaultnboisteault Posts: 4Questions: 1Answers: 0

    Just deactivate "serverSide" and the problem will be gone.

    You mean you disable "serverSide", call draw() then enable "serverSide"? I've looked at the API and did not found how to do that.

    The code look like this:

    $(aTable).dataTable({
        serverSide: true
        , ajax: {
            url: datatablesUrl + '?' + new URLSearchParams(params).toString(),
        }
        , columns: columns
        , createdRow: (row, data) => {
            if ((lConfig['selectedFeatures'].includes(data.DT_RowId.toString()))) {
                row.classList.add('selected');
                data.lizSelected = 'a';
            }
        }
    });
    

    But the draw call is not done in createdRow. It is done elsewhere when some selected state changes.

  • rf1234rf1234 Posts: 3,079Questions: 89Answers: 427

    You mean you disable "serverSide", call draw() then enable "serverSide"? I've looked at the API and did not found how to do that.

    No, I meant to disable "serverSide" completely. Do you really need it? Most people don't really need it - and it can complicate things a lot.

  • nboisteaultnboisteault Posts: 4Questions: 1Answers: 0

    We really need it as we want to handle tables with million of rows.

  • rf1234rf1234 Posts: 3,079Questions: 89Answers: 427

    And do you feel a delay or anything noticeable due to the additional ajax call? I tested it myself and it didn't cause any client side delays to be honest.

  • allanallan Posts: 64,223Questions: 1Answers: 10,598 Site admin
    Answer ✓

    Is it possible to execute the createdRow callback or refresh the table without sending an ajax request?

    To answer this specific question, the answer is no. If you have server-side processing enabled, and that's fair enough with millions of rows, any draw is going to trigger an Ajax request. That's the whole point of server-side processing - a draw requests what to display from the server.

    If you need to update classes based on a local state that can change without needed a draw, do it using the API. Iterate over the rows (rows().every() and row().node() for example) and update the classes as needed.

    It does mean that you'll have the logic both in createdRow and the loop, but I'm sure that could be pulled into a custom function to ensure code reuse.

    Allan

Sign In or Register to comment.