Paginates the data. Useful for e.g. paging bars
Pagination is performed in a way so the current page stays always in the middle.
Example: Lets say we have 100 records and want to page them with 10 records on each page. The current selected page is 5
and we only want to display 3 pages at one time (note: we take the current page from querystring as most pages implement it like this):
<%
'first we get the current page from querystring and parse it so its always a number
currentPage = str.parse(page.QS("page"), 0)
pages = ((new DataContainer)(data)).paginate(10, currentPage, 3)
'results in an array which contains the following values
'... 4 5 6 ...
%>
Now we could create a paging bar out of this array now (there are more solutions but this is the most common).
Note: The "..." indicates that there are more pages. In this example there are more before 4th page and more after 6th page.
<%
link = "<a href=""default.asp?page={0}"">{1}<a/>"
str.write(str.format(link, array(1, "<<")))
str.write(str.format(link, array(currentPage - 1, "< prev")))
for each p in pages
p = str.parse(p, 0)
display = lib.iif(currentPage = p, "<strong>" & p & "</strong>", p)
str.write(lib.iif(p > 0), str.format(link, array(p, display)), "...")
next
str.write(str.format(link, array(currentPage + 1, "next >")))
str.write(str.format(link, array(empty, ">>")))
%>
This code results in a paging which allows us navigating our data by going directly to a page,
jumping to first/last page and moving to next/previous page. Moreover it also displays "..." if there are more pages
and puts the currentpage in a strong-tag.
recsPerPage int
how many records are being displayed per page. Provide 0 if all.
byVal currentPage int
holds the current active page.
only numbers greater 0 are recognized (otherwise 1 is used).
use empty to set the current page to the last page
if the number is higher than the actual last page then its adjusted to the last page
numberOfPages int
indicates how many pages should be returned in total (incl. current page)
number should be odd so the currentpage is always centered. If even then it will be rounded up to the next odd number.
Returns array
returns an array which contains the calculated page numbers in ascending order.
The amount of pages is always equal or or less then numberOfPages
It may contain a '...' marker on FIRST position to indicate there are more pages BEFORE those returned
It may contain a '...' marker on LAST position to indicate there are more pages AFTER those returned.
The array is never empty.
It never contains a 0 value
Possible returns:
There are only 3 pages
[1, 2, 3]
There are pages before the 3rd page and pages after the 5th
["...", 3, 4, 5, "..."]
There are only page before or only after
["...", 3, 4, 5]
[1, 2, 3, "..."]
only one page
[1]