javascript - 重新加载以显示新保存的记录时,数据表未正确重新初始化
问题描述
我有一个数据表,其中填充了数据库中的数据。当网页加载时,我调用我的 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;
});
请协助我如何正确重新初始化数据表。
解决方案
尝试将 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;
});
推荐阅读
- flutter - 权限被拒绝时出现 Flutter_blue 错误
- asp.net-core - 如何访问/传递来自不同范围的数据
- postgresql - 如何从 postgresql 恢复数据库'postgres'
- javascript - 有没有什么方法可以直接在 Python 中使用 xtermjs 解析器而无需前端渲染?
- android - 德尔福安卓;使网格中的列适合其内容
- math - 我有 0-5 范围内的值,但我需要将它们从 1-5 范围转换
- makefile - cmake:如何使用输出创建命名自定义目标,因此如果输出存在则不会构建
- r - 编辑 R 图上轴上的刻度线标签
- azure - 更改 Azure 分析服务上的导入数据库
- python - Python3:在 docker 容器中运行时 subprocess.call() 找不到文件