For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /dolan/pagination.md.

Pagination

Let users move through pages of results with a row of page numbers between previous and next arrows.

Installation

npx @blotch/dolan add pagination
import { UiPagination } from '../components/ui/pagination';

This also adds Icon, whose theme colors draw the arrows.

Usage

import { Component, signal } from '@angular/core';
import { UiPagination } from '../components/ui/pagination';

@Component({
  selector: 'app-results-footer',
  imports: [UiPagination],
  template: `<ui-pagination [(page)]="page" [totalPages]="12" />`,
})
export class ResultsFooter {
  protected readonly page = signal(1);
}
  • [(page)] two-way binds the current page. Pages count from 1.
  • Tapping a number goes to that page. The arrows step one page back or forward, and dim at the first and last page.
  • The first and last pages always show. Around the current page, siblingCount pages show on each side, and ... fills the gaps.
  • With 5 + 2 × siblingCount pages or fewer, every page shows and there are no gaps.
  • The arrows draw in the theme's foreground, like the numbers. For dark mode, see Icon.

Show the current page's items

Derive the visible slice from the page signal with computed(). It updates whenever the user changes page.

protected readonly pageSize = 20;
protected readonly items = signal<readonly Item[]>([]);
protected readonly page = signal(1);

protected readonly totalPages = computed(() =>
  Math.max(1, Math.ceil(this.items().length / this.pageSize)),
);
protected readonly pageItems = computed(() => {
  const start = (this.page() - 1) * this.pageSize;
  return this.items().slice(start, start + this.pageSize);
});
<ui-pagination [(page)]="page" [totalPages]="totalPages()" />

More neighbors

siblingCount sets how many pages show on each side of the current one. Fewer fit a narrow screen better.

<ui-pagination [(page)]="page" [totalPages]="50" [siblingCount]="2" />

Screen readers

Every arrow and number is a button with a label: "Previous page", "Next page", "Page 3", and "Page 4, current page" for the current one. The ... gaps are skipped. Change the labels, for example to translate them, with previousLabel, nextLabel and pageLabel:

<ui-pagination
  [(page)]="page"
  [totalPages]="12"
  previousLabel="Earlier results"
  nextLabel="Later results"
  [pageLabel]="pageLabel"
/>
protected readonly pageLabel = (page: number, current: boolean) =>
  current ? `Results page ${page}, showing now` : `Results page ${page}`;

React to a change

Listen to pageChange to run code when the user changes page. It fires inside the tap, so it can change signals or navigate, as any tap handler can.

<ui-pagination [(page)]="page" [totalPages]="12" (pageChange)="scrollToTop()" />

API

UiPagination

InputTypeDefaultDescription
pagenumber1The current page, from 1. Supports [(page)].
totalPagesnumberRequiredThe number of pages.
siblingCountnumber1Pages shown on each side of the current one.
previousLabelstring'Previous page'What a screen reader says for the previous arrow.
nextLabelstring'Next page'What a screen reader says for the next arrow.
pageLabel(page: number, current: boolean) => stringPage 3, Page 4, current pageWhat a screen reader says for a page number.
classstring''Extra Tailwind classes for the row.
OutputTypeDescription
pageChangenumberEmits the new page when the user taps a number or an arrow. Part of [(page)].

The component doesn't clamp page. If you set it yourself, keep it between 1 and totalPages.

  • List: infinite scroll instead of pages
  • Empty State: what to show when there are no results