重新加载,显示新的记录保存数据表时,不能正确重新初始化

问题描述 投票:4回答:1

我有那里得到与数据库中的数据填充的DataTable。当网页加载我打电话给我的bind()函数,该函数用数据填充数据表,以及它初始化数据表。

我也有一个模式弹出,用户可以添加记录。当他们点击保存按钮,这样可以节省的记录,我们试图重新填充和重新初始化数据表。

问题是,当我重新初始化DataTable中的第二次(点击按钮),DataTable不得到适当的重新初始化。它显示了标题中的每个记录后(见下图)。请注意,这只是发生在按钮上点击。

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

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

请帮助我如何能够正确地重新初始化数据表。

enter image description here

javascript c# jquery asp.net datatable
1个回答
0
投票

尽量存放在变量的数据表,并重新绑定数据之前销毁。

因此,首先创建一个可以在这两个函数访问的变量。

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;
});
© www.soinside.com 2019 - 2024. All rights reserved.