SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Pagination

  1. #1
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination

    Creating Pagination script.
    Trying to do: Displaying 1-10 of 26

    The below works except the a2 logic is off if they are on the last age...

    thoughts?

    //totalRows = all the records.
    //pageNum = the current page their on.
    //pageSize = how many rows to display per page.

    var a1 = (pageSize * pageNum) - pageSize + 1;
    var a2 = (pageSize * pageNum) ;

    "Displaying " + a1 + "-" + a2 + " of " + totalRows

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Edit:

    Ignore this post, Raffles has a better idea below


    Try this formula for a2.
    a2 = (pageSize * pageNum < totalRows) ? pageSize * pageNum : totalRows;

    pageNum = 3
    a1 = (10 * 3) - 10 + 1 = 21
    a2 = (10 * 3) < 26 ? 10 * 3 : 26 = 26

    The ternary operation is a handy shortcut for:
    Code javascript:
    if (pageSize * pageNum < totalRows) {
        a2 = pageSize * pageNum;
    } else {
        a2 = totalRows;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You just want the smallest of either the total number or the page number x the number of entries per page.
    Code:
    var a2 = Math.min(pageSize * pageNum, totalRows);

  4. #4
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha, both work, but Raffles that's pretty sneaky, me likey...

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I discovered it many moons ago after writing my own min() function, then thinking "there must be something like this" and then found out about the wonders of the Math object.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •