jquery Datatables插件-重新加载 AJAX 而不丢失页面时出错

svmlkihl  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(102)

我有一个问题与数据表插件,因为我需要重新加载 AJAX 数据而不丢失当前页面
我知道

table.ajax.reload(null,false)

字符串
将工作,但是,如果我在最后一页,用户删除所有行,有一个错误,因为没有更多的元素在该页面上,我不知道如何纠正错误
我试着:

table.ajax.reload(null,false)

emeijp43

emeijp431#

从我自己的观点来看,除了使用table.ajax.reload(null, false),您可以使用不同的方法。
你可以这样做:

let currentPage = table.page(); // To get the current page number
table.ajax.reload(function(data){
    // Success callback
    if (data.length === 0 && currentPage > 0) {
        // Incase there are no more rows
        table.page(currentPage - 1).draw('page'); // Go to the previous page and redraw the table
    } else {
        table.page(currentPage).draw('page'); // Reload the current page
    }
}, false);

字符串
通过检查是否没有更多的行(data.length === 0),并且当前页不是第一页(currentPage > 0),您可以处理用户删除最后一页上所有行的场景。在这种情况下,代码移动到前一页(table.page(currentPage - 1))并重新绘制表(draw('page'))。否则,如果仍然有行或者当前页是第一页,它只会重新加载当前页(table.page(currentPage).draw('page'))
让我知道如果这工作!

相关问题