1. ## 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. 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;
}```

3. 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. Haha, both work, but Raffles that's pretty sneaky, me likey...

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

#### Posting Permissions

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