如何在DataTables中重新加载/刷新javascript数组

时间:2017-03-14 18:44:25

标签: jquery datatables

我正在使用DataTables来显示搜索功能中的数据。我确定这是否是将数据传递到DataTables的预期方法。我正在用一个空白的javascript数组初始化DataTable。然后我想在不同的函数中填充数据表。

我已经尝试了$()。DataTable()。ajax.reload()和$()。DataTable()。fnAddData(dataSet)但两者都不起作用。

https://jsfiddle.net/owxz7e22/3/

<table id="test" class="table table-striped table-bordered table-hover dt-responsive">
</table>

<a href="#" onclick="LoadData();">Test</a>


$(document).ready(function ()
    {

        $('#test').DataTable({
            data: dataSet,
            columns: [
                { title: "Name" },
                { title: "Position" },
                { title: "Office" },
                { title: "Extn." },
                { title: "Start date" },
                { title: "Salary" }
            ]
        });
    });

    var dataSet = [];

    function LoadData()
    {
        dataSet = [
                ["Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
                ["Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750"],
                ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"],
        ];

        var tbl = $('#test').DataTable();

        tbl.fnClearTable();
        tbl.fnDraw();
        tbl.fnAddData(dataSet);


        //$('#test').DataTable().ajax.reload();

    }

2 个答案:

答案 0 :(得分:4)

您必须将数据表存储在变量中并添加行: 看这里: https://jsfiddle.net/63235xk2/

$(document).ready(function ()
        {
            var dataSet = [];

            var datatable = $('#test').DataTable({
                data: dataSet,
                columns: [
                    { title: "Name" },
                    { title: "Position" },
                    { title: "Office" },
                    { title: "Extn." },
                    { title: "Start date" },
                    { title: "Salary" }
                ]
            });


      $('.asd').on('click',function(){
                dataSet = [
                    ["Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
                    ["Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750"],
                    ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"],
          ];

        alert(dataSet.length);

          datatable.clear();
          datatable.rows.add(dataSet);
          datatable.draw();
      });

        });

答案 1 :(得分:0)

通过定义ajax回调函数,可以使用ajax.reload def net_input(self, x): return np.dot(x, self.w_[1:].T) + self.w_[0] 函数简单地从数组或任何其他数据源加载数据。

此外,此解决方案保持现有行的状态。

示例:

datatable