Pagination
PreviewPage links or buttons for a list that is split into pages.
Features
Permalink to heading "Features"- Takes the paginator of TYPO3's pagination API
- Renders links that reload the page, or buttons that change the page on the client
- Shows the first and last pages and an ellipsis for the pages in between
- Labels every page and trigger for screen readers
<primitives:pagination.root>
<primitives:pagination.firstTrigger />
<primitives:pagination.prevTrigger />
<primitives:pagination.item />
<primitives:pagination.ellipsis />
<primitives:pagination.itemTemplate />
<primitives:pagination.ellipsisTemplate />
<primitives:pagination.nextTrigger />
<primitives:pagination.lastTrigger />
</primitives:pagination.root>
The context of the root holds the pages to show, each with a type (page or ellipsis), a value and an index. Loop over them with ui:context:
<ui:pagination.root count="120">
<ui:context name="ui:pagination" as="paginationContext" />
<ui:pagination.prevTrigger />
<f:for each="{paginationContext.pages}" as="page">
<f:if condition="{page.type} == 'page'">
<f:then><ui:pagination.item value="{page.value}" /></f:then>
<f:else><ui:pagination.ellipsis index="{page.index}" /></f:else>
</f:if>
</f:for>
<primitives:pagination.itemTemplate>
<ui:pagination.item />
</primitives:pagination.itemTemplate>
<primitives:pagination.ellipsisTemplate>
<ui:pagination.ellipsis />
</primitives:pagination.ellipsisTemplate>
<ui:pagination.nextTrigger />
</ui:pagination.root>
An item without content shows its page number, an ellipsis shows …. With itemTemplate and ellipsisTemplate, the client renders the pages when the page changes without a reload.
Installation
Permalink to heading "Installation"typo3 ui:add pagination
Please copy the files manually from GitHub into your project: Pagination
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Paginate a TYPO3 list
Permalink to heading "Paginate a TYPO3 list"Pass the paginator of your list action to paginator, not a TYPO3 pagination: the root works out which pages to show. For links, wrap it in a UrlAwarePaginator with a function that builds the URL of a page, for example with the UriBuilder, which adds the cHash. The root builds the URLs of the pages it shows.
public function listAction(int $currentPage = 1): ResponseInterface
{
$paginator = new UrlAwarePaginator(
new QueryResultPaginator($this->eventRepository->findAll(), $currentPage, 4),
fn(int $page): string => $this->uriBuilder->reset()
->setSection('event-list')
->uriFor(null, ['currentPage' => $page]),
);
$this->view->assign('paginator', $paginator);
return $this->htmlResponse();
}
- Wine tastingCologne
- Guided city tourCologne
- Jazz eveningCologne
- Farmers marketCologne
Load pages without a reload
Permalink to heading "Load pages without a reload"Pass your endpoint to fetchUrl, here the same list action with format json, render the root with autoMount="{false}" and mount it yourself with a loadPage function. It loads and shows a page after every page change: pass its signal to the request, so a response of a page that is no longer current is dropped. A click on a link no longer loads its URL: the address bar follows the page, and back and forward change it. For that, the server sends the URL of every page to the client.
public function listAction(int $currentPage = 1): ResponseInterface
{
$paginator = new UrlAwarePaginator(/* as above */);
if ($this->request->getFormat() === 'json') {
$events = array_map(
static fn(Event $event): array => ['title' => $event->getTitle(), 'city' => $event->getCity()],
[...$paginator->getPaginatedItems()],
);
return $this->jsonResponse(json_encode(['events' => $events]));
}
$this->view->assign('paginator', $paginator);
return $this->htmlResponse();
}
Show a window of pages
Permalink to heading "Show a window of pages"With boundaryCount="0" and no ellipsis in the loop, the pagination shows a window of pages around the current one. siblingCount sets its width.
Jump to the first and last page
Permalink to heading "Jump to the first and last page"Add firstTrigger and lastTrigger.
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives. For a full list of available client side props and methods, see the Zag.js Machine API.
pagination.root
Permalink to heading "pagination.root"Provides the pages and labels the navigation. Renders a <nav> element.
| Name | Description | Required | Default |
|---|---|---|---|
paginator | TYPO3\CMS\Core\Pagination\PaginatorInterfaceThe TYPO3 paginator of the list, e.g. an `ArrayPaginator` or a `QueryResultPaginator`. Wrap it in a `UrlAwarePaginator` for links. It sets the current page and the number of pages. | No | - |
count | integerThe total number of items, which sets the number of pages together with `defaultPageSize`. Leave it out with a `paginator`. | No | - |
defaultPage | integerThe page that is current when rendered, 1 by default. Leave it out with a `paginator`, which sets it. | No | - |
defaultPageSize | integerThe number of items per page. With a `paginator`, set it to the items per page of the paginator, so the page size in `onPageChange` matches. | No | 10 |
siblingCount | integerThe number of pages shown on each side of the current page. | No | 1 |
boundaryCount | integerThe number of pages always shown at the start and at the end. | No | 1 |
pageUrls | arrayThe URL of every page, keyed by the page number starting at 1, for links without a `UrlAwarePaginator`. Leave it out with one. With it, the items and triggers are links to their page. | No | - |
fetchUrl | stringThe endpoint your client script loads the content of a page from: the library passes it to your `loadPage` and sends no request itself. With links, a click on a page no longer loads it: the URL in the address bar follows the page, and back and forward change it. Buttons have no URL, so the history stays as it is. | No | - |
translations | arrayLocalized labels: `rootLabel`, `firstTriggerLabel`, `prevTriggerLabel`, `nextTriggerLabel`, `lastTriggerLabel` and `itemLabel`, which takes a `%page%` and a `%totalPages%` placeholder. A client script can pass Zag's `itemLabel` function instead. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
provider | stringThe `id` of the `.provider` this root takes its `rootId` and props from. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
pagination.item
Permalink to heading "pagination.item"Goes to one page. Renders an <a> element with a UrlAwarePaginator or pageUrls, a <button> element otherwise.
| Name | Description | Required | Default |
|---|---|---|---|
value | integerThe page number of the item. Without content, the item shows it. Left out in the `itemTemplate`, where the client sets it. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-pagination-item | <uid> |
data-index | The index of the item |
data-selected | Present when selected |
pagination.ellipsis
Permalink to heading "pagination.ellipsis"Stands for the pages that are left out. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
index | integerThe position of the ellipsis among the pages, the `index` of its entry in `pages`. | No | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-pagination-ellipsis | <uid> |
data-index | The index of the item |
pagination.itemTemplate
Permalink to heading "pagination.itemTemplate"Holds the item the client clones for a page it renders. Renders a <template> element.
pagination.ellipsisTemplate
Permalink to heading "pagination.ellipsisTemplate"Holds the ellipsis the client clones. Renders a <template> element.
pagination.firstTrigger
Permalink to heading "pagination.firstTrigger"Goes to the first page. Renders an <a> or a <button> element, as the item.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-pagination-first-trigger | <uid> |
data-disabled | Present when disabled |
pagination.prevTrigger
Permalink to heading "pagination.prevTrigger"Goes to the previous page. Renders an <a> or a <button> element, as the item.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-pagination-prev-trigger | <uid> |
data-disabled | Present when disabled |
pagination.nextTrigger
Permalink to heading "pagination.nextTrigger"Goes to the next page. Renders an <a> or a <button> element, as the item.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-pagination-next-trigger | <uid> |
data-disabled | Present when disabled |
pagination.lastTrigger
Permalink to heading "pagination.lastTrigger"Goes to the last page. Renders an <a> or a <button> element, as the item.
| Name | Description | Required | Default |
|---|---|---|---|
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-pagination-last-trigger | <uid> |
data-disabled | Present when disabled |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
page | number | The current page. |
count | number | The total number of data items. |
pageSize | number | The number of data items per page. |
totalPages | number | The total number of pages. |
pages | Pages | The page range. Represented as an array of page numbers (including ellipsis) |
previousPage | number | null | The previous page. |
nextPage | number | null | The next page. |
pageRange | PageRange | The page range. Represented as an object with `start` and `end` properties. |
slice | <V>(data: V[]) => V[] | Function to slice an array of data based on the current page. |
setPageSize | (size: number) => void | Function to set the page size. |
setPage | (page: number) => void | Function to set the current page. |
goToNextPage | VoidFunction | Function to go to the next page. |
goToPrevPage | VoidFunction | Function to go to the previous page. |
goToFirstPage | VoidFunction | Function to go to the first page. |
goToLastPage | VoidFunction | Function to go to the last page. |
getItemState | (props: ItemProps) => ItemState | Returns the state of a pagination item |