简体   繁体   English

如何使用javascript显示分页?

[英]how to show pagination with javascript?

This code show this type of pagination 这段代码显示了这种分页

1 2 3 4 5 6 7 8 9 10 11 12 next>>

I want show want show only first two digits and last And next button. 我要显示只显示前两位数字和最后一个以及下一个按钮。 Does first 2 values need to change on next click. 下次点击时前两个值是否需要更改?

1 2 .... 12 next>>

This is my code: 这是我的代码:

_paginationOutput: function(currentPage, totalPages) {
            this.writeDebug('_paginationOutput',arguments);

            currentPage = parseFloat(currentPage);
            var output = '';
            var nextPage = currentPage + 1;
            var prevPage = currentPage - 1;

            // Previous page
            if( currentPage > 0 ) {
                output += '<li class="bh-sl-next-prev" data-page="' + prevPage + '">' + this.settings.prevPage + '</li>';
            }

            // Add the numbers
            for (var p = 0; p < Math.ceil(totalPages); p++) {
                var n = p + 1;

                if (p === currentPage) {
                    output += '<li class="bh-sl-current" data-page="' + p + '">' + n + '</li>';
                }
                else {
                    output += '<li data-page="' + p + '">' + n + '</li>';
                }
            }

            // Next page
            if( nextPage < totalPages ) {
                output += '<li class="bh-sl-next-prev" data-page="' + nextPage + '">' + this.settings.nextPage + '</li>';
            }

            return output;
        },

You'll want to check if the p variable is one of the pages you would like ignored. 您将要检查p变量是否为您要忽略的页面之一。

EDIT: To add the ... , put it in the if condition, and check if you already printed the dot. 编辑:要添加... ,将其放在if条件中,并检查是否已经打印了该点。

Try this code. 试试这个代码。 Put this in the beginning of the for loop. 将其放在for循环的开头。

And, create a new variable called dotsAdded before the for loop. 并且,在for循环之前创建一个新的变量,称为dotsAdded

if (p >= 2 && p < totalPages - 1) {

    if (!dotsAdded) {
        output += "...";
        dotsAdded = true;
    }

    continue;
}

That will continue the loop if and only if p is between 2 and the "second to last page." 当且仅当p在2与“倒数第二页”之间时,这将继续循环。

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

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