Why data is not updated

Why data is not updated

I have the following code :

    var drivers = [];
    $("#tblDrivers").dataTable({
        "lengthMenu": [[25, 50, 100], [25, 50, 100]],
        data: drivers,
        columns: [
            {},
            { data: 'Name' },
            { data: 'CompanyName' },
            { data: 'PhoneNo' },
            { data: 'Email' },
            { data: 'Truck' },
            { data: 'Trailer' },
            { data: 'IsVistracksAdded' },
        ]
    });

then I get data and want to update table:

        driverSignalR.client.onGetDrivers = function (data) {
            drivers = data;
            var table = $('#tblDrivers').DataTable();
            table.data = drivers;
            table.rows().invalidate().draw();
        }

new data is coming (I see it in the debugger), but nothing happened on client side. What I do wrong?

Answers

  • kthorngrenkthorngren Posts: 21,141Questions: 26Answers: 4,918

    What format is your data in?

    Instead of using this:

        table.data = drivers;
        table.rows().invalidate().draw();
    

    Depending on the format of drivers you should just use rows.add().

    Kevin

  • magicscreenshot@gmail.commagicscreenshot@gmail.com Posts: 3Questions: 2Answers: 0
    edited February 2018

    It works if I call
    table.draw();
    after rows.add();

    very unclear API....

  • kthorngrenkthorngren Posts: 21,141Questions: 26Answers: 4,918

    There is a lot to the API to understand.

    You can also use:
    rows.add().draw();

    Kevin

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    You need to call draw() when you want the table to update for the latest configuration (as noted in the rows.add() documentation) so that you can optionally queue up multiple changes before drawing the table to allow for better performance.

    Allan

This discussion has been closed.