【发布时间】:2023-03-03 18:10:01
【问题描述】:
我有一个小的 jQuery 数据表,它会定期逐一更新它的行。这是通过用“更新”行替换该行、回调服务器、接收作为 html 的行并用服务器提供的新 html 替换“更新”行来完成的。这有效,用户会看到更新的信息出现。
但是,如果用户随后单击列标题,则表中的行将返回到“更新”状态,在更新过程中会持续几毫秒。
这是我检索行并尝试使其内容无效的 javascript,以便 DataTables 将排序(并在排序后显示)该行的最新 replaceWith() 版本。
function getRow(svcName, rowId)
{
// Mark in progress
$("#" + rowId).html("<td colspan='5'>" + svcName + "<i class='fa fa-refresh fa-spin fa-1x fa-fw'></i></td>").addClass('info');
// Get the HTML
$.ajax(
{
type: 'get',
dataType: 'html',
url: svcName,
error: function (jqXHR, textStatus, errorText) {
$("#" + rowId).text("Error " + errorText + " (" + svcName + ")");
var row = $(".service-table").DataTable().row("#" + rowId);
row.invalidate('dom');
},
success: function (response, textStatus, jqXHR) {
// This response is visible in the table
// UNTIL I sort the table, at which time it's the temporary
// version of the row, above, which is restored
// from the dataTable cache.
$("." + rowId).replaceWith(response);
var row = $(".service-table").DataTable().row("#" + rowId);
row.invalidate('dom');
//row.draw('row');
}
}
);
}
如何强制 dataTable 知道我已经替换了行?
更新:我已按照 RickL 的答案更新了代码,但看到了类似的行为:
function getRow(svcName, rowId)
{
// Mark in progress
$("#" + rowId).html("<td colspan='5'>" + svcName + "<i class='fa fa-refresh fa-spin fa-1x fa-fw'></i></td>").addClass('info');
// Get the HTML
$.ajax(
{
type: 'get',
dataType: 'html',
url: svcName,
success: function (response, textStatus, jqXHR) {
var table = $(".service-table:first").DataTable();
// get the index
var rowIndex = table.row("#" + rowId).index();
// get the jQuery representation of the row and replace
var $row = $(table.row(rowIndex).node());
$row.replaceWith(response);
table.row(rowIndex).invalidate(); //.draw(false);
}
}
);
}
【问题讨论】:
标签: jquery datatables