我正在使用 fnDeleteRow 删除jQuery数据表中的特定行。问题是如果用户所在的页面不同于1,则在删除该行之后,用户返回到第1页,尽管他/她删除了另一页中的项目,例如:4。
var datatable_test = $("#datatable_test").dataTable({
"bStateSave": true,
"bLengthChange": true,
"bFilter": true,
"bInfo": true,
"bPaginate": true,
"bAutoWidth": false,
"aaSorting": [],
"aoColumnDefs": [{
"aTargets": [-1],
"bSortable": false,
"bSearchable": false,
"mRender": function(data){
return "<a href="#" class="action_delete" id="delete_" + data + "">delete</span>";
}
}],
"bProcessing": true
});
$(".action_delete").click(function(e){
e.preventDefault();
var row = $(this).closest("tr").get(0);
var id = $(this).attr("id");
$.post("helper.php", { action: "delete", id: id }).done(function(){
$(row).fadeOut("fast", function(){
datatable_test.fnDeleteRow(datatable_test.fnGetPosition(row));
});
});
});
删除项目后,用户应保留在第4页。有什么想法吗?
答案 0 :(得分:3)
如果有人有兴趣,我自己找到解决方案:
$.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings ) {
return {
"iStart": oSettings._iDisplayStart,
"iEnd": oSettings.fnDisplayEnd(),
"iLength": oSettings._iDisplayLength,
"iTotal": oSettings.fnRecordsTotal(),
"iFilteredTotal": oSettings.fnRecordsDisplay(),
"iPage": Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
"iTotalPages": Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
};
}
然后:
var page_number = datatable_test.fnPagingInfo().iPage;
datatable_test.fnDeleteRow(datatable_test.fnGetPosition(row), function(){datatable_test.fnPageChange(page_number);}, false);
答案 1 :(得分:0)
<强>替换强>
datatable_test.fnDeleteRow(datatable_test.fnGetPosition(row));
。通过强>
var DataTable_Test= $("#datatable_test").DataTable();
DataTable_Test.row(row).remove().draw( false );