如何在dataTable更新期间替换/删除行?

时间:2017-03-07 20:08:11

标签: javascript jquery datatables datatables-1.10

所以,我正在处理使用DataTables API的代码。看来它正在使用它的旧版本。因此,我的表每5秒更新一次,我需要的是它在新的新数据进入时完全更新。但是,当它输入新的更新时,该表中不再引用的数据会保留在那里,即使它应该消失,随着时间的推移,我的桌子变得越来越大。我试图使用:

Table.fnClearTable(); // throws error below
TypeError: Cannot set property '_aData' of undefined

这不起作用。它删除了所有内容,但不允许我重建一个新表。我也尝试过使用:

Table.fnDestroy(); // doesn't seem to work either
"bDestroy":true // didn't seem to work, but I couldn't tell

所以,这是我正在努力的特殊功能。我想定期删除并重新创建表格,或者更好地刷新进入它的数据。

my.updating.graph = function(obj, listAll) {
    try {
    if (obj == null || obj.updates == null || obj.updates.length == 0) {
        $("#sectionUpdates").hide();
        return;
    }
    $("#sectionUpdates").show();
    var table = null;
    if ($.fn.dataTable.isDataTable("updateTable")) { // may remove this statement...
        table = $("#updateTable").dataTable();
    } else {
        table = $("#updateTable").dataTable({ // is there a reset option?
            "paging": !listAll,
            "pagingType": !listAll ? "X" : "Y",
            "searching": true,
            "order": [
                [0, "desc"]
            ],
            "ordering": true,
            "oLanguage": {
                "sEmptyTable": "No updates at this time"
            },
            "iDisplayLength": 15,
            "info": false,
            "formatNumber": function(format) {
                return friendlyFormat(format, 1000, 0);
            },
            "aoColumns": [{
                    "bVisible": false
                },
                {
                    "className": "dt-center",
                    "width": "10%",
                    "iDataSort": 0,
                    "render": function(data, type, row) {
                        return extractData(data, type, row[0]);
                    }
                }, {
                    "sType": "mixed-string",
                    "className": "dt-left",
                    "width": "30%",
                    "render": function(data, type, row) {
                        return getZData(data, type, row[1]);
                    }
                }, {
                    "className": "dt-left",
                    "width": "30%",
                    "render": function(data, type, row) {
                        return getYData(data, type, row[2]);
                    }
                }, {
                    "sType": "mixed-string",
                    "className": "dt-left",
                    "width": "30%",
                    "render": function(data, type, row) {
                        return getZData(data, type, row[3]);
                    }
                }
            ]
        });
        $("#updateTable").resize(function() {});
    }
    var updates = obj.updates;
    for (var i = 0; i < updates.length; i++) {
        var update = updates[i];
        var id = makeId(update.id);
        var triggered = getCurrentUTC();
        if (update) {
            if (getID("updateTable", id) == null) {
                var row = table.fnAddData([
                    update.field1,
                    update.field2,
                    update.field3,
                    update.field4
                ]);
                var node = $("#updateTable").dataTable().fnSettings().aoData[row[0]].nTr;
                node.setAttribute("id", "updateTable-item-" + id);
                node.setAttribute("rowIdx", row[0]);
                setID("updateTable", id, row[0]);
            }
            table.fnUpdate([
                    update.field1,
                    update.field2,
                    update.field3,
                    update.field4
                ], getID("updateTable", id),
                null, false, false);
            for (var q = 0; q < tblStatusClasses.length; q++) {
                $("updateTable-item-" + id + " td:eq(0)").removeClass(tblStatusClasses[q]);
            }
            $("#updateTable-item-" + id + " td:eq(0)").addClass("table-state" + update.field1);
        }
        table.fnStandingRedraw();
    }
} catch (e) {
    console.log(e);
}
}

0 个答案:

没有答案