Skip to content

Pagination ​

When there are many rows, show N at a time and add a page bar (the pager) below the grid. Client mode keeps every row in memory and the grid cuts the pages. Server mode only holds the current page, which you fetch from your server.

Live demo — drawn by the real renderer

Get started ​

Pass a single pagination option. Page numbers start at 1.

ts
const options: GridOptions<Row> = {
  columns,
  rows, // every row
  rowKey: 'id',
  pagination: { mode: 'client', pageSize: 10, pageSizes: [10, 20, 50] },
};
OptionMeaning
mode'client' (the grid cuts the pages) or 'server' (your server does)
pageSizeRows per page (an integer, 1 or more)
pageSizesChoices for the page size. With two or more, the pager shows a select box
pageThe page to show at start (or when you change it from outside)
totalRowsTotal count in server mode. With loadPage, the value in its answer replaces it
loadPageServer mode: a function the grid calls when the page changes
position'bottom' (default), 'top', or 'none' (no pager, API only)
jumpA box for typing a page number. Off by default
unsavedServer mode: block page moves while edits are unsaved ('block', default) or drop them ('discard')
messagesOverride the pager texts. The default follows locale (Korean or English)

The container needs a height. The pager sits outside the grid (below or above) and the grid fills the remaining height.

Choose the pager style ​

Since 1.2.0

The pager option is available since 1.2.0. The 1.1.0 pager has one style.

The samples below assume the columns and rows you built earlier and the api the grid returned. Row is the row type.

pagination.pager changes only how the pager looks. Page math, the API and the events stay the same.

ts
const options: GridOptions<Row> = {
  columns,
  rows,
  rowKey: 'id',
  pagination: { mode: 'client', pageSize: 10, pageSizes: [10, 25], pager: 'simple' },
};

// Change the style later. The current page and the rows per page stay as they are
api.setOptions({
  pagination: { mode: 'client', pageSize: 10, pageSizes: [10, 25], pager: 'button' },
});
ValueWhat you see
'full'First, previous, numbers, next, last. On narrow screens it shrinks to "n / m". Default
'simple'Previous, next, "Page n of m", the summary and the rows-per-page select
'button'Previous, numbered buttons (… when there are many), next. Keeps the numbers on narrow screens
  • The "n of m" text of simple is messages.pageOf, with {page} and {total} (the page count). The default is Page {page} of {total} in English and {page} / {total} 쪽 in Korean. To change the wording, pass for example messages: { pageOf: 'Page {page}/{total}' }.
  • A value other than 'full', 'simple' or 'button' raises invalid-option and the current style stays.
  • All controls are native buttons, so Tab, Enter and Space work. Focus stays on the button you pressed, so repeating Enter keeps turning pages. Tab walks through grid cells one by one, so press F6 to move focus from the grid to the pager (to the current page button) and from the pager back to the grid.
  • The current number button has aria-current="page". Previous is disabled on the first page and next on the last. With no data it reads "1 / 1".
  • As before, tree and grouping cannot be used together with paging.

See the gallery example paging-pager-modes.

Customize the pager ​

Change the text, the number of page buttons and the layout with pagination options. Anything you leave out behaves as before.

ts
pagination: {
  mode: 'client',
  pageSize: 10,
  summary: (info) => `Page ${info.page}/${info.pageCount} · ${info.totalRows} rows`, // your own summary text
  pageButtons: 5,        // page-number slots (3-15, even numbers round up to odd). Default: 5 or 7 by width
  sizeSelect: true,      // page-size select: default only when pageSizes has 2+, true always, false hidden
  parts: ['summary', 'nav'],   // visible parts and their order: summary | status | size | nav | jump
  formatNumber: (n) => String(n),  // page numbers and counts
}
  • If summary throws or returns a non-string, the default text is used and an error event (callback-error) is emitted.
  • Parts missing from parts are removed from the DOM; the others follow the array order. status carries the Retry button. The summary is still announced to screen readers (aria-live).
  • autoPageSize: true sets the rows per page from the grid body height (floor((height - header) / rowHeight), at least 1) and recalculates when the size changes. It needs a numeric rowHeight (a function is an error) and a grid with a fixed height. While on, pageSizes and the size select are not used.

Move pages in code ​

api.nextPage(), prevPage(), firstPage() and lastPage() return true when the page changed (pageChanged reason api). They return false at the edge, or when unsaved changes block the move in server mode. In server mode lastPage() returns false while the total is unknown.

API and events ​

ts
api.getPage(); // current page (from 1)
api.setPage(3); // go to page 3. Past the last page goes to the last page
api.setPageSize(25); // change size. Keeps the page that holds the row you were viewing
api.getPageCount();
api.getPageInfo(); // { page, pageSize, pageCount, totalRows, from, to, loading, error }
await api.reloadPage(); // server mode: request the current page again

api.on('pageChanged', ({ page, pageSize, pageCount, totalRows, reason }) => {});
api.on('pageLoadChanged', ({ loading, error }) => {}); // server request state

reason tells why the page changed: api, user (pager), size, sort, filter, data (rows shrank), options, reload.

An invalid page such as 0 or a fraction throws invalid-option and leaves the state untouched.

The visible rows (getVisibleRows(), getVisibleRowCount()) are the rows of the current page. Read the total with getPageInfo().totalRows.

Server paging ​

The grid holds only the current page. loadPage receives the page, size, sort and filter and returns { rows, totalRows }.

Live demo — drawn by the real renderer
ts
pagination: {
  mode: 'server',
  pageSize: 10,
  loadPage: async ({ page, pageSize, sort, filter, signal }) => {
    const res = await fetch(`/api/orders?page=${page}&size=${pageSize}`, { signal });
    const { rows, totalRows } = await res.json();
    return { rows, totalRows };
  },
},
  • Several changes in the same tick send one request (changing the sort also returns to page 1).
  • When a newer request starts, the older request's signal is aborted. Pass it to fetch as is. A late answer to an old request is dropped.
  • A wrong answer shape or a failed request raises an error event (page-load-failed) and sets error in pageLoadChanged. The pager shows a "Retry" button.
  • Pass only totalRows, without loadPage, to fill the rows yourself. Listen to pageChanged and call setRows.
Live demo — drawn by the real renderer

Block a page change ​

When hooks.beforePageChange({ from, to, pageSize, source }) returns false, the page does not move (the pager buttons stay as they were). Server paging total updates and the return to page 1 after a sort or filter are not asked. The rules are in the hooks table.

With sort and filter ​

Changing the sort or filter returns to page 1, so a filter applied on page 5 never leaves you on a page that no longer exists. Client mode cuts pages from all rows after filter and sort. Server mode calls loadPage again with the new conditions.

Live demo — drawn by the real renderer

Selection, editing and batch save ​

  • Selection is remembered by row key, so in client mode it stays across pages. The header checkbox selects only the current page. In server mode the rows are replaced, so the selection is cleared.
  • Editing and change tracking keep adding up across pages in client mode, and one save() sends everything.
  • In server mode with unsaved edits, a page move is blocked by default (unsaved: 'block') with a page-blocked notice. With 'discard' the edits are dropped and the page moves. A sort or filter change fetches fresh data, so it can drop edits too.
Live demo — drawn by the real renderer
Live demo — drawn by the real renderer

Summary row and export ​

The summary row (summary) totals all rows that pass the filter and appears on every page. For the total of the current page only, add up getVisibleRows() yourself. CSV export uses the visible rows, that is the current page. For everything, build it from getRows().

Live demo — drawn by the real renderer

Row numbers and accessibility ​

  • Numbers from showRowNumbers continue across pages (the first row on page 2 is 11).
  • The grid's aria-rowcount is the total count plus the header, and each row's aria-rowindex is its overall position.
  • The pager is a nav landmark and the current page number has aria-current="page". The summary text is aria-live, so a page change is announced.
  • Every control is a button, select or input: move with Tab, activate with Enter or Space.
  • Below 480px of width the number list is replaced by n / N.

More to try ​

Live demo — drawn by the real renderer
Live demo — drawn by the real renderer
Live demo — drawn by the real renderer
Live demo — drawn by the real renderer
Live demo — drawn by the real renderer

Limits ​

  • It cannot be combined with a tree or grouping (tree, grouping). Passing both throws invalid-option. Paging by top-level group does not exist today (roadmap).
  • In server mode, moving to another page replaces the rows, so the selection is cleared.

Choosing features ​

maxgrid/core leaves paging out. Register maxgrid/pagination. See Choosing features for the details.

ts
import { createGrid } from 'maxgrid/core';
import { pagination } from 'maxgrid/pagination';

createGrid(el, {
  columns,
  rows,
  pagination: { mode: 'client', pageSize: 20 },
  features: [pagination],
});