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.
Get started
Pass a single pagination option. Page numbers start at 1.
const options: GridOptions<Row> = {
columns,
rows, // every row
rowKey: 'id',
pagination: { mode: 'client', pageSize: 10, pageSizes: [10, 20, 50] },
};| Option | Meaning |
|---|---|
mode | 'client' (the grid cuts the pages) or 'server' (your server does) |
pageSize | Rows per page (an integer, 1 or more) |
pageSizes | Choices for the page size. With two or more, the pager shows a select box |
page | The page to show at start (or when you change it from outside) |
totalRows | Total count in server mode. With loadPage, the value in its answer replaces it |
loadPage | Server mode: a function the grid calls when the page changes |
position | 'bottom' (default), 'top', or 'none' (no pager, API only) |
jump | A box for typing a page number. Off by default |
unsaved | Server mode: block page moves while edits are unsaved ('block', default) or drop them ('discard') |
messages | Override 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.
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' },
});| Value | What 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
simpleismessages.pageOf, with{page}and{total}(the page count). The default isPage {page} of {total}in English and{page} / {total} 쪽in Korean. To change the wording, pass for examplemessages: { pageOf: 'Page {page}/{total}' }. - A value other than
'full','simple'or'button'raisesinvalid-optionand 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,
treeandgroupingcannot 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.
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
summarythrows or returns a non-string, the default text is used and anerrorevent (callback-error) is emitted. - Parts missing from
partsare removed from the DOM; the others follow the array order.statuscarries the Retry button. The summary is still announced to screen readers (aria-live). autoPageSize: truesets 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 numericrowHeight(a function is an error) and a grid with a fixed height. While on,pageSizesand 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
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 statereason 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 }.
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
signalis aborted. Pass it tofetchas is. A late answer to an old request is dropped. - A wrong answer shape or a failed request raises an
errorevent (page-load-failed) and setserrorinpageLoadChanged. The pager shows a "Retry" button. - Pass only
totalRows, withoutloadPage, to fill the rows yourself. Listen topageChangedand callsetRows.
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.
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 apage-blockednotice. With'discard'the edits are dropped and the page moves. A sort or filter change fetches fresh data, so it can drop edits too.
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().
Row numbers and accessibility
- Numbers from
showRowNumberscontinue across pages (the first row on page 2 is 11). - The grid's
aria-rowcountis the total count plus the header, and each row'saria-rowindexis its overall position. - The pager is a
navlandmark and the current page number hasaria-current="page". The summary text isaria-live, so a page change is announced. - Every control is a
button,selectorinput: move withTab, activate withEnterorSpace. - Below 480px of width the number list is replaced by
n / N.
More to try
Limits
- It cannot be combined with a tree or grouping (
tree,grouping). Passing both throwsinvalid-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.
import { createGrid } from 'maxgrid/core';
import { pagination } from 'maxgrid/pagination';
createGrid(el, {
columns,
rows,
pagination: { mode: 'client', pageSize: 20 },
features: [pagination],
});