SpreadsheetGrid
Guides

Pagination and loading display

Split rows into pages with pagination and show one page at a time; show skeleton rows or a moving line while loading with loading

With pagination, the displayed rows are split into pages of pageSize rows and shown one page at a time. The right of the default bottom bar shows the row range 「1–50 / 1234 行」 ("1–50 of 1234" in English), page numbers and the rows-per-page choice. loading is the look while your app is loading rows: skeleton rows when there are none, a dimmed table and a moving line when there are. Both are available since 1.15.0.

Each page still uses virtual scrolling, so the grid can show a million rows without pages (Large data). Use pagination for tables where people talk about "row N of page M", or when you want a look closer to printed reports.

Demo (1,234 orders > 50 rows per page)

Rows 1234 of 1234Columns 6 of 6Sort: —
#
Order no.
1
J-10001
2
J-10002
3
J-10003
4
J-10004
5
J-10005
6
J-10006
7
J-10007
8
J-10008
9
J-10009
10
J-10010
11
J-10011
12
J-10012
13
J-10013
14
J-10014
15
J-10015
16
J-10016
17
J-10017
18
J-10018
19
J-10019
20
J-10020
21
J-10021
Rows 1234 of 1234Columns 6 of 6
Cell —Range —0 cells / 0 rows0 columns

Sorting by a header or filtering by customer goes back to page 1. Find (Ctrl+F) searches every page.

  • Change pages with ‹ › and the numbers at the right of the bottom bar. "Rows per page" picks 20 / 50 / 100 rows per page.
  • Sorting by a header or filtering by "Customer" goes back to page 1.
  • Arrow keys, Ctrl+End, Ctrl+A, copy and paste stay within the current page. Find (Ctrl+F) searches every page and switches to the page of the found cell.
  • "Fit to height" makes a page as many rows as fit in the visible height of the body (autoPageSize).
  • With "Hide the bottom bar", the pager appears in a band below the table. Make the table narrow and the page number buttons become "3 of 25"; narrower still, the row range and the rows-per-page choice are hidden. The pager keeps its width when the page changes.
  • "Reload" keeps the rows, dims the table and shows a line under the column headers; "Load from scratch" empties the rows and shows skeleton rows (rows arrive after 1.5 seconds in both cases).

Usage

<SpreadsheetGrid
  rows={rows}
  onRowsChange={setRows}
  columns={columns}
  rowKeyGetter={(row) => row.id}
  pagination={{ pageSize: 50 }}
  onPageChange={(info) => console.log(`${info.page + 1} / ${info.pageCount}`)}
/>

pagination={true} keeps the defaults (50 rows per page; rows per page offers 20 / 50 / 100).

OptionDefaultWhat it does
pageSize50Rows per page. Changing it later also resets rows per page to this value and shows the page containing the current page's first row.
pageSizeOptions[20, 50, 100]The numbers offered for rows per page. false hides the choice. If the current rows per page isn't among them, it is added.
autoPageSizefalseMakes a page as many rows as fit in the visible height of the body (recounted when the table's height changes; the rows-per-page choice isn't shown). It doesn't work in tables whose row heights vary (autoHeight), which are split by pageSize instead.

onPageChange is called with GridPageInfo ({ page, pageCount, pageSize, rowCount, firstRow }; page starts at 0) when the page or rows per page change. It isn't called on the first render.

What stays in the page and what spans pages

What gets split are the displayed rows after filtering, sorting and opening / closing row groups or trees. Group rows and label rows count as one row each (detail row bands don't).

Only within the pageAcross all pages
Arrow keys, PageUp / PageDown, Ctrl+Home / Ctrl+EndFind (find) and replace (switches to the page of the found cell)
Range selection, Ctrl+A, copy, paste, DeleteUndo (goes back to the page of the changed cell), redo (goes back to where undo was pressed)
The fill handle, row draggingRow selection with the header checkbox
Export scope 'page'Export scope 'view' (default)
Total row aggregates, the bottom bar count "Rows 1234 of 1234"
  • Changing filtering or sorting goes back to the first page. When rows decrease and the current page no longer exists, the last page is shown.
  • Changing the page commits a value being edited and scrolls the body back to the top. If there is an active cell, it is put on the same column of the new page's first row.
  • There is no key to change pages (use the pager buttons or the handle's setPage).
  • The total row and pinned rows appear on every page. The entry row appears only on the last page.
  • When filtering or sorting takes you back to the first page, the active cell stays at the same position in the page (the same as sorting a table without pagination).
  • Pasting stops at the end of the page, and rows that overflow aren't pasted (treated like the end of the table, so they aren't in onPasteSkipped). On the last page it works as before (rows are added if there is createRow).
  • Cut (Ctrl+X) also stays within the page. If you move to another page and paste, the original cells aren't cleared and it becomes a normal paste.
  • The count chip of the bottom bar ("Rows 1234 of 1234" = data rows) and the pager's row range ("1–50 of 1240" = displayed rows, counting group rows and label rows) differ when there are group rows or label rows.
  • When you filter rows with conditions outside the grid, the grid doesn't change the page. Call setPage(0) when the conditions change to go back to the first page.

Row numbers are continuous across pages

The handle's viewRowIndex, ctx.rowIndex of callbacks and renderCell, canEditCell, row numbers and aria-rowindex are continuous across all pages, not positions within the page (with 50 rows per page, the first row of page 2 is 50). Pointing at a row on another page makes the grid switch to that page first.

const grid = useRef<SpreadsheetGridHandle<Order>>(null);

// Find a row by key, switch to its page and select it
const index = grid.current?.getViewRowIndex(orderId) ?? -1;
if (index >= 0) grid.current?.selectCell(index, 1, { scrollIntoView: true });

grid.current?.setPage(0); // to page 1
grid.current?.setPageSize(100); // 100 rows per page
grid.current?.getPageInfo(); // { page, pageCount, pageSize, rowCount, firstRow } (null without pagination)

setPageSize does nothing on an autoPageSize grid (the number follows the grid's height).

onPageChange is called only when the page or the rows per page change (not when only the row count or the page count changes; read the current numbers with getPageInfo()).

When the height of an autoPageSize table changes, it shows the page containing the first row from when the height started changing (or the active cell's row, if any); restoring the height brings back the original page. The body's height is measured after rendering, so HTML rendered on the server and the first render are split by pageSize.

Remembering the page (state)

getState() includes page and pageSize, so you can come back to the same page the same way as saving and restoring state. Changing the page also calls onStateChange.

<SpreadsheetGrid
  pagination
  initialState={saved}
  onStateChange={(state) => localStorage.setItem('orders-grid', JSON.stringify(state))}
  // …
/>

A page given before rows are loaded (while there are 0 rows) is kept as is, and pulled into range once rows arrive. Meanwhile page in getState() is the page you gave, and moving to that page when the rows arrive doesn't call onPageChange / onStateChange (the pager shows the first page).

Drawing the pager in your own bottom bar

In tables that replace the bar with renderBottomBar, the grid doesn't draw a pager. Draw one yourself with ctx.pagination (GridPaginationContext = the current page plus setPage / setPageSize). Tables without a bottom bar (showBottomBar={false}) get the pager in a band below the table from the grid.

<SpreadsheetGrid
  pagination
  renderBottomBar={(ctx) => (
    <div className="my-statusbar">
      <span>{ctx.derivedSummary.rowSummaryText}</span>
      <button
        disabled={!ctx.pagination || ctx.pagination.page === 0}
        onClick={() => ctx.pagination?.setPage(ctx.pagination.page - 1)}
      >
        Previous
      </button>
      <span>
        {(ctx.pagination?.page ?? 0) + 1} / {ctx.pagination?.pageCount ?? 1}
      </span>
      <button
        disabled={!ctx.pagination || ctx.pagination.page >= ctx.pagination.pageCount - 1}
        onClick={() => ctx.pagination?.setPage(ctx.pagination.page + 1)}
      >
        Next
      </button>
    </div>
  )}
  // …
/>

With serverSide

Tables with dataSource work too. The server's row count is split into pages, and getRows is asked only for the blocks in the current page's range.

Loading display (loading)

Fetching rows stays outside the grid as before. Set loading to true only while you are fetching.

const [rows, setRows] = useState<Order[]>([]);
const [loading, setLoading] = useState(true);

useEffect(() => {
  fetchOrders().then((data) => {
    setRows(data);
    setLoading(false);
  });
}, []);

<SpreadsheetGrid rows={rows} onRowsChange={setRows} columns={columns} loading={loading} />;
  • With no rows (the first load): skeleton rows fill the height of the body, and "No rows to show" isn't shown. The bottom bar shows "Loading…" where the counts are.
  • With rows (reloading): the current rows stay, the table is dimmed and a moving line appears under the column headers. Meanwhile editing, pasting, Delete, the fill handle and replacing are stopped (selecting, copying, scrolling, sorting and filtering still work). An editor that was open when loading started stays open and writes its value when committed (new edits don't start).
  • The counts in the top and bottom bars, and the pager's row range (when there are no rows), show "Loading…".
  • The table gets aria-busy="true" and the root gets .ssg-root--loading. Colors use the same tokens as serverSide refetching (serverSideRefetch="keep"): --ssg-refetch-row-opacity / --ssg-refetch-bar / --ssg-refetch-bar-track.
  • serverSide (dataSource) tables don't need it, because the grid shows loading rows itself.

With /lite

Pagination is the feature part paginationFeature (not in standardFeatures). loading is in the core, so it works without a part.

import { createSpreadsheetGrid, standardFeatures, paginationFeature } from '@ishibashi0112/spreadsheet-grid/lite';

const Grid = createSpreadsheetGrid({ features: { ...standardFeatures, pagination: paginationFeature } });

On this page