我对DataTables有一个奇怪的问题。我正在使用这些东西来删除行并且一切正常(行从DB中删除,警报正确显示),当我点击上一页并返回到最初显示行的页面时,它会从显示中删除行(按页面)我的意思是DataTable页码或我每页增加的行数) - 它再次显示...(因为它实际上没有删除)
仅在硬刷新后才会删除。为什么会发生这种情况以及如何在不实际刷新浏览器的情况下真正删除它?
以下是我用来删除行的代码:
function DeletePublisher(element, publisherid) {
jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function (r) {
if (r) $.post('includes/publishers/delete-publisher.php?publisherid=' + publisherid,
function (data) {
if ($.trim(data) == 'error') {
$.jGrowl('This publisher was already deleted', {
header: '<strong style="text-align:center;">ERROR</strong>'
});
$(element)
.parents('tr')
.remove();
} else {
$(element)
.parents('tr')
.remove();
oTable.fnDeleteRow(element);
$('tr')
.removeClass();
// Now add class names again
$('tr:odd')
.addClass('odd');
$('tr:even')
.addClass('even');
$.jGrowl('Publisher deleted');
}
});
});
这是TR元素:
<td class="action-th">
<ul class="button-table-head">
<li><div class="button-head edit-icon"><a href="/includes/publishers/edit-publisher.php?editpublisher=<?php echo $publisher_id; ?>" data-fancybox-type="iframe" class="iframe sweet-tooltip" data-text-tooltip="Edit" data-style-tooltip="tooltip-mini-slick"><span>Edit</span></a></div></li>
<li><div class="button-head delete-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Delete" data-style-tooltip="tooltip-mini-slick" onclick="DeletePublisher(this,'<?php echo $publisher_id; ?>')"><span>Delete</span></a></div></li>
</ul>
</td>
所以不确定元素是否在第一个位置传递了正确的行ID?
答案 0 :(得分:1)
您可能希望尝试$(element).parents('tr').remove();
从数据表中删除行,而不是使用fnDeleteRow
从DOM中删除该元素。您可以将参数传递给方法,该方法将告诉Datatables重绘表格,这不会强制您回发以刷新数据。
这是Datatables API中的the link到fnDeleteRow
。
<强>更新强>
尝试这样的事情:
function DeletePublisher(element, publisherid) {
jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function (r) {
if (r) $.post('includes/publishers/delete-publisher.php?publisherid=' + publisherid,
function (data) {
var nTr = $(element).closest('tr');
oTable.fnDeleteRow(nTr, null, true);
if ($.trim(data) == 'error') {
$.jGrowl('This publisher was already deleted', {
header: '<strong style="text-align:center;">ERROR</strong>'
});
} else {
$.jGrowl('Publisher deleted');
}
});
});