Paged View Links Macro

Last modified by Thomas Mortagne on 2024/07/05 18:03

cogGenerates links that can be used in a paged view.
TypeDoc (Velocity Macro)
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

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

NameOptionalAllowed valuesDefault valueDescription
itemsPerPagenoany integer-The number of displayed items per page.
totalPagesnoany integer-The total number of pages, starting from 0 (so 0 = 1 page, 1 = 2 pages).
crtPageNumbernoany 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)

Result

pagedLinks.png

Get Connected