【问题标题】:Datatable does not reinitializes properly when reloaded to display new saved record重新加载以显示新保存的记录时,数据表未正确重新初始化
【发布时间】:2019-06-27 09:26:26
【问题描述】:

我有一个数据表,其中填充了数据库中的数据。当网页加载时,我调用我的 Bind() 函数,该函数用数据填充数据表并初始化数据表。

我还有一个模式弹出窗口,用户可以在其中添加记录。当他们单击保存按钮时,它会保存记录,我会尝试重新填充并重新初始化数据表。

问题是当我第二次重新初始化数据表时(点击按钮),数据表没有正确地重新初始化。它在每条记录之后显示标题(见下图)。请注意,这只发生在单击按钮时。

到目前为止这是我的代码:

$(document).ready(function () {
    var dataTable;
    Bind();

    function Bind() {
        $.ajax({
            type: "POST",
            url: "Clubs.aspx/GetCustomers",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });
    }

function OnSuccess(response) {
    var xmlDoc = $.parseXML(response.d);
    var xml = $(xmlDoc);
    var customers = xml.find("Table");
    var row = $("[id*=gvCustomers] tr:last-child").clone(true);
    $("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
    $.each(customers, function () {
        var customer = $(this);
        $("td", row).eq(0).html($(this).find("ClubID").text());
        $("td", row).eq(1).html($(this).find("ClubName").text());
        $("td", row).eq(2).html($(this).find("ClubEmail").text());
        $("td", row).eq(3).html("<span>" + $(this).find("ClubID").text() + "</span>");
        $("td", row).eq(4).html("<img id='btnID' src='/assets/img/edit.png' Style='width: 20px; height: 18px;'</>");
        $("[id*=gvCustomers]").append(row);
        row = $("[id*=gvCustomers] tr:last-child").clone(true);
    });
    //This is where I initializes my datatable
    $('table').each(function () {
        dataTable = $(this).prepend($("<thead style=background-color:#ff974d></thead>").append($(this).find("tr:eq(0)"))).DataTable({
            "responsive": true,
            "searching": true,
            "aaSorting": [],
            "sPaginationType": "full_numbers",
            dom: 'lBfrtip',
            buttons: [
                {
                    extend: 'copy',
                    className: 'btn btn-success'
                },
                {
                    extend: 'excel',
                    text: 'Excel',
                    title: 'Centre Information',
                    className: 'btn btn-success',
                    orientation: 'portrait',
                    exportOptions: {
                        columns: [0, 1, 2]
                    }
                },
                {
                    extend: 'pdf',
                    text: 'PDF',
                    title: 'Centre Information',
                    className: 'btn btn-success',
                    orientation: 'portrait',
                    exportOptions: {
                        columns: [0, 1, 2]
                    }
                },
                {
                    extend: 'print',
                    text: 'PRINT',
                    title: 'Centre Information',
                    className: 'btn btn-success',
                    orientation: 'portrait',
                    exportOptions: {
                        columns: [0, 1, 2]
                    }
                }
            ]
        });
    });
}
    });

然后这是我尝试重新初始化数据表的保存按钮中的代码

$("#btnSave").click(function () {
    dataTable.destroy();
    Bind();
    return false;
});

请协助我如何正确重新初始化数据表。

【问题讨论】:

    标签: javascript c# jquery asp.net datatable


    【解决方案1】:

    尝试将 DataTable 存储在一个变量中,并在重新绑定数据之前将其销毁。

    所以首先创建一个可以在两个函数中访问的变量。

    var dataTable;
    
    function Bind() {
        //rest of code
    }
    

    然后将正确的表赋给变量

    dataTable = $(this).prepend($("<thead style=background-color:#ff974d></thead>").append($(this).find("tr:eq(0)"))).DataTable({
    

    然后在那个按钮上点击销毁

    $("#btnSave").click(function () {
        dataTable.destroy();
        Bind();
        return false;
    });
    

    【讨论】:

    • 好的,所以我尝试了这个,但它在每一行之后添加了标题列。但是,至少现在我可以看到记录,但就像我说的那样,它会在每条记录之后不断添加标题列。知道如何解决这个问题吗?
    • 不确定你的意思。我已经使用没有 tje Ajax 的简单 html 表进行了测试。它没有添加额外的标题行。所以问题可能出在OnSuccess 函数中。您有示例 xml 吗?
    • 我现在去看看,如果我发现了什么,我会提供更多反馈
    • 请查看包含您的建议的更新代码。
    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多