当数据源是一个数组时,jQuery DataTables刷新网格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当数据源是一个数组时,jQuery DataTables刷新网格相关的知识,希望对你有一定的参考价值。

我有一个数据表,每次单击按钮都应该刷新。

通过单击此按钮,我将使用ajax从API加载JSON流。数据将被处理,然后在数据表中设置为数据源。

我不能使用数据表的ajax内置功能。

代码如下(为简单起见,数据处理函数已被删除):

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);
        }
    });
}

当用户单击某些按钮并传递不同的categoryID值时,将调用上述函数,从而从API加载不同的数据。

从API返回的数据如下所示:

{
    "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", "", "", "", ""]
    ]
}

在第一次加载时,数据表工作正常,我可以看到数据。

在第二次单击时,ajax调用有效,因为我可以在网络选项卡中看到响应,但数据表未更新。我已经尝试了文档中的所有建议,但它们都不起作用,它们通常是指数据表的ajax内置功能。

答案

尝试

table.rows.add(response.data); 

代替

table.data(response.data);

或破坏并重新初始化数据表。

以上是关于当数据源是一个数组时,jQuery DataTables刷新网格的主要内容,如果未能解决你的问题,请参考以下文章

c#中datagridview中datasource反复赋值没有变化?

你如何使用 jQuery Deferreds 数组? [复制]

如何正确设计数据表?

我想知道为啥当我发送一个数组以使用 jquery ajax (django,jquery) 查看时发生错误

过滤 JQuery 数据表后检索行数据

当动态值匹配时,jQuery 按类对元素进行分组