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

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:

```
if (pageSize * pageNum < totalRows) {
a2 = pageSize * pageNum;
} else {
a2 = totalRows;
}
```

You just want the smallest of either the total number or the page number x the number of entries per page.

```
var a2 = Math.min(pageSize * pageNum, totalRows);
```

Haha, both work, but Raffles that’s pretty sneaky, me likey…

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.