如何从网格数据表插件 jQuery 中清除数据

how to clear data from grid datatable plugin jquery?

本文关键字:jQuery 清除 数据 插件 数据表 网格      更新时间:2023-09-26

我们使用数据表插件如下来绑定数据并显示在网格中。

productJs = {
apiBaseUrl: "/api/Admin/GetProductsPurchasedOrInCart",
dataTableID: "#products_Grid",
ready: function () {
    alert('yyyy');
},
jqueryDatatableSetting: {
    "aaSorting": [],
    "aoColumns": [
     { "mDataProp": "ProductName" },
     { "mDataProp": "Quantity" },
     { "mDataProp": "unitPrice" }
    ]
},
jqueryDatatable: null,
bindJqueryControls: function (e) {
},
loadProducts: function (oderNo) {
    //alert(oderNo);
    var apiUrl = this.apiBaseUrl + '?IsPurchased=Y&OrderNo=' + oderNo;
    $.ajax({
        url: apiUrl,
        type: "GET",
        complete: function (resp) {
            if (resp.status == 200 || resp.status == 201) {
                $('#products_Grid > tbody').html('');
                productJs.bindDatatable(resp.responseJSON);
            } else if (resp.status == 404 || resp.status == 400) {
            }
        },
        error: function () {
        }
    });
}
, bindDatatable: function (records) {
    if ($("#products_Grid_wrapper").length == 0) {
        //it doesn't exist
        this.productdataTableID = $(this.dataTableID).dataTable(this.jqueryDatatableSetting);
    } else {
        var table = $(this.dataTableID).DataTable();
        table.clear().draw();
    }
    if (records.length)
        this.productdataTableID.fnAddData(records);
 }
}

让我解释一下发生了什么:我有一个网格绑定 t''in 与上面 '' 相同,即 orderJs。我们希望当用户单击订单网格中的"详细信息"按钮时显示订单详细信息。订单网格也在使用数据表,因为我正在绑定产品的数据。

现在我做了什么: 我已经根据产品网格中的订单号完成了数据的绑定,它第一次工作正常。 意味着当我单击"订单详细信息"时,它会打开带有正确产品的弹出窗口。 现在我已经关闭了弹出窗口。 现在单击任何其他订单详细信息按钮, 现在,当弹出窗口打开时,它会获取旧订单的产品+新订单的产品。

我想在重新绑定产品网格之前将其清除。

尝试了几件事,但没有运气,你们专家可以吗,请检查我做错了。

嗨,

我得到了答案.. 哇..

我刚刚通过添加"fnClearTable"函数更新了我的以下方法a

bindDatatable: function (records) {
    if ($("#products_Grid_wrapper").length == 0) {
        //it doesn't exist
        this.jqueryproductDatatable = $(this.dataTableID).dataTable(this.jqueryDatatableSetting);
    } else {
        this.jqueryproductDatatable.fnClearTable();
    }
    if (records.length) {
        this.jqueryproductDatatable.fnAddData(records);
    }
}

感谢大家的支持... :)