manu
manu

Reputation: 103

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;
        },

Upvotes: 1

Views: 95

Answers (1)

Kayla
Kayla

Reputation: 485

You'll want to check if the p variable is one of the pages you would like ignored.

EDIT: To add the ..., put it in the if condition, and check if you already printed the dot.

Try this code. Put this in the beginning of the for loop.

And, create a new variable called dotsAdded before the for loop.

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."

Upvotes: 1

Related Questions