Paged View Links Macro
Last modified by Thomas Mortagne on 2024/07/05 18:03
Generates links that can be used in a paged view. |
Type | Doc (Velocity Macro) |
Category | |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Generates links that can be used in a paged view, i.e. search results, list of objects, list of documents, etc. It uses two request parameters, "ipp" (items per page) and "page" (the current page to display).
All the parameters should be numbers.
If there is only one page, then the links are not printed.
It's up to you to implement the logic to display the current page. See the example below.
Usage
#pagedViewLinks(itemsPerPage totalPages crtPageNumber)
Parameters definition
Name | Optional | Allowed values | Default value | Description |
---|---|---|---|---|
itemsPerPage | no | any integer | - | The number of displayed items per page. |
totalPages | no | any integer | - | The total number of pages, starting from 0 (so 0 = 1 page, 1 = 2 pages). |
crtPageNumber | no | any integer | - | The currently displayed page, starting from 0 (so 0 = first page, 1 = second page). |
Example
1 Paged Documents for space
## The full data to display
#set ($spaceDocs = $xwiki.getSpaceDocsName("Main"))
#set($itemsPerPage = 5)
#if($request.ipp)
#set($itemsPerPage = $xwiki.parseInt($request.ipp))
#end
#set($pageNumber = 0)
#if($request.page)
#set($pageNumber = $xwiki.parseInt($request.page))
#end
#set($startAt = $itemsPerPage * $pageNumber)
#if($startAt < 0)
#set($startAt = 0)
#end
#set($endAt = $itemsPerPage * ($pageNumber + 1))
#if($endAt > $spaceDocs.size())
#set($endAt = $spaceDocs.size())
#end
#if($startAt > $endAt)
#set($startAt = $endAt)
#end
#set($totalPages = ($spaceDocs.size() - 1) / $itemsPerPage)
#set($crtPageNumber = $startAt / $itemsPerPage)
#set($spaceDocsInPage = $spaceDocs.subList($startAt, $endAt))
{table}
Document Name
#foreach($item in $spaceDocsInPage)
$item
#end
{table}
#pagedViewLinks($itemsPerPage $totalPages $crtPageNumber)
## The full data to display
#set ($spaceDocs = $xwiki.getSpaceDocsName("Main"))
#set($itemsPerPage = 5)
#if($request.ipp)
#set($itemsPerPage = $xwiki.parseInt($request.ipp))
#end
#set($pageNumber = 0)
#if($request.page)
#set($pageNumber = $xwiki.parseInt($request.page))
#end
#set($startAt = $itemsPerPage * $pageNumber)
#if($startAt < 0)
#set($startAt = 0)
#end
#set($endAt = $itemsPerPage * ($pageNumber + 1))
#if($endAt > $spaceDocs.size())
#set($endAt = $spaceDocs.size())
#end
#if($startAt > $endAt)
#set($startAt = $endAt)
#end
#set($totalPages = ($spaceDocs.size() - 1) / $itemsPerPage)
#set($crtPageNumber = $startAt / $itemsPerPage)
#set($spaceDocsInPage = $spaceDocs.subList($startAt, $endAt))
{table}
Document Name
#foreach($item in $spaceDocsInPage)
$item
#end
{table}
#pagedViewLinks($itemsPerPage $totalPages $crtPageNumber)