简体   繁体   English

如何更改DataTables jQuery插件的分页按钮数量

[英]How to change the number of pagination buttons of the DataTables jQuery plugin

By default, DataTables plugin shows 7 paging buttons (including the ellipses) like默认情况下, DataTables插件显示 7 个分页按钮(包括省略号),例如

Previous 1 2 3 4 5 ... 10 Next上一页1 2 3 4 5 ... 10下一页

I would like to be able to change this to a smaller number like我希望能够将其更改为较小的数字,例如

Previous 1 ... 10 Next上一页1 ... 10下一页

and I can't find this anywhere in the documentation.我在文档中的任何地方都找不到这个。

I found this plugin but it says that's deprecated and that the我找到了这个插件,但它说它已被弃用,并且

DataTables 1.10 has this ability built in. DataTables 1.10 内置了此功能。

but it doesn't show where to change this.但它没有显示在哪里改变这一点。

I finally found it after fiddling with the DataTable javascript object and the DataTables ' source code.在摆弄DataTable javascript 对象和DataTables的源代码后,我终于找到了它。

You have to add this line (either before or after initialization) :您必须添加此行(在初始化之前或之后)

$.fn.DataTable.ext.pager.numbers_length = 3;

Note that this will show up like请注意,这将显示为

Previous 1 ... 10 Next上一页1 ... 10下一页

and not并不是

Previous 1 2 ... 10 Next上一页1 2 ... 10下一页

so be sure to include the ellipses in the length number.所以一定要在长度数字中包含省略号。

Edit:编辑:

I saw some problems with this solution when advancing through the pages.在浏览页面时,我发现此解决方案存在一些问题。

I had to rewrite their _numbers function like this:我不得不像这样重写他们的_numbers函数:

function _numbers(page, pages) {
    var
        numbers = [],
        buttons = 5, // added here the number of buttons
        half = Math.floor(buttons / 2);

    if(pages <= buttons) {
        numbers = _range(0, pages);
    } else if(page <= half) {
        numbers = _range(0, buttons - 2);

        numbers.push("ellipsis");
        numbers.push(pages - 1);
    } else if(page >= pages - 1 - half) {
        numbers = _range(pages - (buttons - 2), pages);

        numbers.splice(0, 0, "ellipsis");
        numbers.splice(0, 0, 0);
    } else {
        numbers.push(page); // changed this from _range(page - 1, page + 2);
        numbers.push("ellipsis");
        numbers.push(pages - 1);
        numbers.splice(0, 0, "ellipsis");
        numbers.splice(0, 0, 0);
    }

    numbers.DT_el = "span";

    return numbers;
}

And used this to point out DataTables to my own function:并用它向我自己的函数指出DataTables

$.fn.DataTable.ext.pager.simple_numbers = function(page, pages) {
    return ["previous", _numbers(page, pages), "next"];
};

Also, I had to copy their _range function into my main.js file.另外,我必须将他们的_range函数复制到我的main.js文件中。

显然 DataTableJS 的理想最小值是:

$.fn.DataTable.ext.pager.numbers_length = 5;

Combine set up with https://www.gyrocode.com/articles/jquery-datatables-pagination-without-ellipses/https://www.gyrocode.com/articles/jquery-datatables-pagination-without-ellipses/结合设置

Add the following after the script loaded加载脚本后添加以下内容

<script>
    $.fn.DataTable.ext.pager.numbers_length = 3;
    $.fn.DataTable.ext.pager.full_numbers_no_ellipses = function (e, r) { var a = [], n = $.fn.DataTable.ext.pager.numbers_length, t = Math.floor(n / 2), l = function (e, r) { var a; void 0 === r ? (r = 0, a = e) : (a = r, r = e); for (var n = [], t = r; t < a; t++)n.push(t); return n }; return (a = r <= n ? l(0, r) : e <= t ? l(0, n) : e >= r - 1 - t ? l(r - n, r) : l(e - t, e + t + 1)).DT_el = "span", ["first", "previous", a, "next", "last"] };
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM