I am working on a pagination feature in a web service I am writing, but my lack of math insight is killing me now.
I have a couple of keys: totalItems
, currentItems
, currentPage
, totalPages
, but also a couple of links to first
, last
, previous
and next
.
At this moment, I am doing the following calculations:
totalItems
: number of rows in the tablecurrentItems
:limit
parameter from HTTP requestcurrentPage
:start
parameter divided bylimit
parametertotalPages
: number of rows in the table divided bylimit
. (Rounded up, 8.1 page = 9 pages)
I assume those calculations to be correct, what I am struggling with is the following:
first
:start
parameter is 1 withlimit
from the HTTP requestlast
: should be the first item of the last page, how do I calculate this correctly?previous
: should be the first item of the previous page, how do I do this?next
: should be the first item of the next page, how do I do this?
What I would like to ask, is: are my calculations correct? And how do I tackle the three problems with last
, previous
and next
?