Skip to content
Advertisement

jQuery DataTables refresh the grid when the datasource is an array

I have a datatable that should be refreshed every time I click a button.

By clicking this button I will load, using ajax, a JSON stream from an API. The data will be processed and then set as datasource in the datatables.

I can’t use the ajax builtin feature of datatables.

The code is the following (for simplicity the data process function has been removed):

function loadBulletins(categoryID) {
    $.ajax({
        url: '/api/bulletins.ashx',
        type: "POST",
        dataType: "json",
        data: { method: "getbulletins", iso3: iso3, catid:categoryID },
        success: function (response) {
            if ($.fn.dataTable.isDataTable('#bulletins-table')) {
                table = $('#bulletins-table').DataTable();
                table.clear();
                table.data(response.data); //it seems like the update here doesn't work
                table.draw();  //Table is not updated!!
                //table.ajax.reload();  //it only works with ajax builtin
            }
            else {
                $('#bulletins-table').DataTable({
                    deferRender: true,
                    "bLengthChange": false,
                    responsive: true,
                    "autoWidth": false,
                    "bInfo": false,
                    "bFilter": false,
                    data: response.data
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("loadBulletins Status: " + textStatus, "Error: " + errorThrown);
        }
    });
}

The above function is called when the user clicks on some buttons, passing different categoryID values, therefore loading different data from the API.

The data returned from the API is like the following:

{
    "data": [
        ["8", "Emergency Assessment: Faryab Province", "2", "2018", "00065098/download/", "", "", "", ""],
        ["", "XXX/NRC Emergency Shelter Assessment: Khogiani", "12", "2017", "00050404/download/", "", "", "", ""],
        ["7", "Emergency Market Assessment: Sayad and Qush Tepa Districts", "9", "2017", "000022544/download/", "", "", "", ""],
        ["6", "Emergency Assessment Bulletin: Darz Ab District (Jawzjan) - Rapid Assessment", "7", "2017", "019342/download/", "", "", "", ""],
        ["5", "Emergency Flash Update: Bala Buluk District: Farah Province - Conflict Rapid Assessment", "3", "2017", "1236.pdf", "", "", "", ""],
        ["4", "Emergency Flash Update: Faryab Province - Conflict Rapid Assessment", "1", "2017", "754.pdf", "", "", "", ""],
        ["3", "Emergency Flash Update: Kohistan District (Faryab) - Conflict Rapid Assessment", "11", "2016", "8973.pdf", "", "", "", ""],
        ["2", "Emergency Flash Update: Farah Province - Conflict Rapid Assessment", "11", "2016", "88394.pdf", "", "", "", ""],
        ["1", "Emergency Flash Update: Kunduz Province - Conflict Rapid Assessment", "10", "2016", "88308.pdf", "", "", "", ""]
    ]
}

At the first load the datatable works fine and I can see the data.

At the second click, the ajax call works, as I can see the response in the network tab, but the datatable is not updated. I’ve tried all the suggestions in the documentation but none of them works and they generally refer to the ajax builtin feature of the datatable.

Advertisement

Answer

try

table.rows.add(response.data); 

instead of

table.data(response.data);

or destroy and reinitialize the datatable.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement