首页 > 解决方案 > 如何启用 Datatables 分页按钮以获得无限结果?

问题描述

如何配置Datatable以使分页按钮始终处于启用状态,并触发ajax数据加载方法?

我的数据太大而无法一次加载,因此我使用“ajax”选项的“function”值来调用自定义 JSON 加载方法,该方法可以重复调用以加载下一页结果。

我希望用户单击“上一个”和“下一个”按钮来触发该功能。但是,默认情况下,Datatables 似乎假定“ajax”方法第一次加载所有数据,因此如果结果适合当前表而不需要分页,则分页按钮被禁用。

我尝试使用浏览器的检查器通过 CSS 属性动态启用它们,但看起来这只是一种美化效果,它们实际上并没有触发 ajax 回调。

我该如何解决?这甚至是数据表支持的东西吗?

标签: javascriptjqueryajaxdatatable

解决方案


You should put serverSide option to true to use ajax.

If you want to make pagination and you don't know how many records you have, you should return this in the ajax response { ... "recordsTotal": Length of table, "recordsFiltered": int.MaxValue, ...} and use info: false & pagingType: 'simple' options to hide the number of records and show just Next and Previous buttons without pages.

   dataTable({
        ......
        processing: true,
        serverSide: true,
        pagingType: 'simple',
        info: false,
        lengthMenu: [10, 25, 50, 100, 250, 500, 1000],
        searchDelay: 1000,
        pageLength: 10,
        ajax: {
            url: 'url',
            type: 'get'
        }

推荐阅读