Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Javascript
Tip/Trick

JavaScript: An idea of implementing page bar on webpage

Rate me:
Please Sign up or sign in to vote.
4.93/5 (5 votes)
16 Dec 2016CPOL3 min read 6.5K   2   1
Features : There are NO "Next Page" or "Prev Page" buttons, user just clicks the page buttonAlways showing first page button and the last page button, omitting intermediate ones replaced by an ellipsis.

Introduction

A page tool for website users to switch to next or previous pages. Its use is very clear.

Design

Current page is the first page: no "span" stuff. So does the last page

When the current page is not at either of the extremity, it always appears at the middle of the span which prescribes the length of displaying middle pages:

Displaying the middle pages, meanwhile showing the first and the last

after click another page in the span, it will turn to that page, putting it to the middle of the span.

Current page always in the center

And span can be changed only through a variable(or constant)

A span of 7 pages

Ok then ... how we implement that?

Well, first we estimate possible situations:

(*[number] means the current page, which is the highlighted one in the pics above;

   each number means the white page button in the pics above

   & we suppose the span = 5 the total = 10)

        A:  left(pages including the first page and those in the span) + ... + total

                   [1] 2 3 4 5 6 ... 10

                   1 [2] 3 4 5 6 ... 10

                   1 2 [3] 4 5 6 ... 10 (now a "span" appears)

                   1 2 3 [4] 5 6 ... 10 ("span" begins to move rightward)

        B: 1 + ... + span + ... + total

                   1 ... 3 4 [5] 6 7 ... 10 (cur should be in the center, instead of: 1 ... 3 [4] 5 6 7 ... 10)

                   1 ... 4 5 [6] 7 8 ... 10

        C: 1 + ... + right (whose length is also span+1)

                   1 ... 5 6 [7] 8 9 10

                   1 ... 5 6 7 [8] 9 10

                   1 ... 5 6 7 8 [9] 10

                   1 ... 5 6 7 8 9 [10]

 

        Umm... seems good but if: there is only 7 pages?(<= span + 2)

        D: if total is less than or equal to span,  than 1 + span + total; current is not in the center

                   [1] 2 3 4 5 6 7

                   1 [2] 3 4 5 6 7

                   1 2 [3] 4 5 6 7

                   1 2 3 [4] 5 6 7

                   1 2 3 4 [5] 6 7

                   1 2 3 4 5 [6] 7

                   1 2 3 4 5 6 [7]

 

 This is for the LOGIC part. Pretty foolproof huh? XD

Let's get started with JavaScript!

First, the pseudo code:

  1. Denote the most left page in the "span" as "spanL"; spanL = cur - Math.floor(span/2); And the the most right page in the "span" as "spanR"; spanR = cur + Math.floor(span/2);
    JavaScript
    when spanL >=3, "1... "shows up
    when spanR <= total - 2, "...total" shows up
     
    create three strings to add up: "prefix", "span", and "postfix"
    prefix ='<a href = "javascript:;" class="tcdNumber">1</a>' + '<span> ... </span>';
    span = ''; // to be determined later by spanL and spanR
    postfix = '<span> ... </span>' + '<a href = "javascript:;" class="tcdNumber">'+ total +'</a>' + '<span> ... </span>';
  2. Decide which plan to execute (Plan A, B, C, D are those described above), and reset the span of the "span" (reset the value of spanL and spanR and decide whether to add "1 ..." or "... total")
    JavaScript
    if(total <=span)
        execute Plan D
    else
    {
        if(spanL >=3)
            if(spanR <=total-2)
                execute Plan B
            else
                execute Plan C
        else
            if(spanR <=total-2)
                execute Plan A
            else
                execute Plan D
    }
  3. After establishing which way the page bar will look like(among the 4 ways described above), we add the page buttons in the "span":
    JavaScript
    for(var i = spanL; i < spanR; i++)
    {
        if (i == cur)
            spanToWrite +='<span class="current">' + i + '</span>'; // Highlight the current button
        else
            spanToWrite +='<a href = "javascript:;" class="tcdNumber">' + i + '</a>'; // paint other buttons
    }
  4. Finally we add up all the statements then to write
    obj.append(prefix + spanToWrite + postfix); // html documents written on the webpage

To Implement

In the codes below, we can change the length of "span"(the pages displayed in the middle) just by change the value of PAGE_SPAN (you can also turn it to a variable if you wish) 

JavaScript
return (function ()  {
    obj.empty();
    
    var cur = args.current;
    var total = args.pageCount;
    var spanL = cur - Math.floor(PAGE_SPAN / 2);
    var spanr = cur - Math.floor(PAGE_SPAN / 2);
    var prefix = '<a href="javascript:;" class="tcdNumber">1</a>
        + '<span style="font=weight: 900; font-size:20px; font-color:#1C83BF">...</span?';
    var postfix = 'span style="font-weight: 900; font-size:20px; color::#1C83BF">...</span>'
        + '<a href="javascript:;" class="tcdNumber">' + total + '</a>'
    var spanToWrite = '';
    
    if (total <= PAGE_SPAN)  {
        // condensedInBothSidesSpan
        prefix = '';
        postfix = '';
        spanL = 1;
        spanR = total;
    }
    else {
        if (spanL >= 3) {
            if (spanR <= total - 2) {
                // prefix + span + postfix
                // Need to do nothing. 
            }
            else {
                // prefix + expandedRightSpan
                postfix = '';
                spanL = total - PAGE_SPAN;
                spanR = total;
            }
        }
        else {
            if (spanR <= total - 2) {
                // expandedLeftSpan + postfix
                prefix = '';
                spanL = 1;
                spanR = 1 + PAGE_SPAN;
            }
            else {
                // condensedInBothSidesSpan
                prefix = '';
                postfix = '';
                spanL = 1;
                spanR = total;
            }
        }
    }
    
    for (var i = spanL; i <= spanR; i++) {
        if (i == cur)
            spanToWrite += '<span class="current">' + i + '</span>';
        else
            spanToWrite += '<a href="javascript:;" class="tcdNumber">' + i + '</a>';
    }
    
    obj.append(prefix + spanToWrite + postfix);
})();

Hope it will be helpful! :)

(This is from my google blog: http://splashing-spray.blogspot.com/2016_07_01_archive.html)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
A guy learning .NET C#, ASP.NET, JavaScript, Unity C#, SQL. Glad to be with the community discussing and sharing experience.

Comments and Discussions

 
GeneralMy vote of 5 Pin
FlaviaPoupard18-Dec-16 20:12
FlaviaPoupard18-Dec-16 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.