Select server rows
Keep checked rows across pages, select every matching row of a server result, and send the selection to your API as ids or as a spec.
A server grid holds one page, or the loaded part of a scroll-mode source. Declare the size of the whole result, so the checks survive page changes and "select all matching" covers rows that the grid never loaded.
Check rows on page 1, go to page 3, and check more. Then check the header checkbox: the bar under the grid offers "Select all 237 matching". Uncheck a row and change pages.
ids: []Declare the result
Set totalRowCount to the size of the whole result, or null when the size is unknown. totalRowCount makes checkedRowsLifetime default to 'across-filters', which keeps the checks across page changes and on a cell click. An explicit value overrides the default. totalRowCount also makes sortMode and filterMode default to "server". See Sorting. useDataGridServerRows sets totalRowCount and checkedRowsLifetime="across-filters" in gridProps and providerProps.
A negative or non-finite totalRowCount counts as null. With 'across-filters', a cell selection keeps the checks, and row operations act on the checked rows only. Without totalRowCount, the default follows rowMarkers.
With totalRowCount and checkedRowsLifetime="across-filters" in effect:
- A checked row that leaves
datastays checked by row id, in every mode. A page change, an eviction in scroll mode, and a server sort or filter change keep it. When the row returns, it shows as checked. actions.selectAllMatching()checks every row of the result. Rows that load later show as checked. Unchecking a row excludes it by id. It returns{ ok: true }, or{ ok: false, reason }without a change.
Read the selection as a value
useDataGridSelectedRows() returns the selection as a DataGridSelectedRows value. Send it to your server, which applies the action. fetchRows is the loader from Load rows by page:
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
useDataGridActions,
useDataGridSelectedRows,
} from "@/components/data-grid/data-grid";
import { DataGridPaginationBar } from "@/components/data-grid-pagination/data-grid-pagination";
import { DataGridStatusBar } from "@/components/data-grid-status-bar/data-grid-status-bar";
import { useDataGridServerRows } from "@/components/data-grid-server/data-grid-server";
import { columns, type Order } from "./orders";
import { api, fetchRows } from "./api";
function DeleteSelected({ refresh }: { refresh: () => void }) {
const selected = useDataGridSelectedRows();
const { clearSelection } = useDataGridActions();
const remove = async () => {
await api.deleteOrders(
selected.mode === "ids"
? { ids: selected.ids }
: { spec: selected.spec, exceptIds: selected.excludedIds },
);
clearSelection();
refresh();
};
return <button onClick={remove}>Delete {selected.count ?? "all"}</button>;
}
export function OrdersGrid() {
const server = useDataGridServerRows<Order>({
fetchRows,
});
return (
<DataGridProvider
{...server.providerProps}
columns={columns}
rowMarkers="checkbox"
>
<DataGridRoot {...server.rootProps} className="h-96">
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
<DataGridPaginationBar {...server.pager} />
<DataGridStatusBar />
<DeleteSelected refresh={server.refresh} />
</DataGridProvider>
);
}In ids mode, ids lists the checked loaded rows and the remembered ones. Unloaded rows have no id and stay out. In allMatching mode, excludedIds lists the unchecked rows, and spec holds the sort, the filters, and the search text of the grid. count is totalRowCount minus the exclusions, or null when the size is unknown.
The server applies the action to every row that matches spec, except the excluded ids. This route treats the body as untrusted input. It bounds each id list to the 100,000 ids that the grid keeps, and parseServerSpec checks the spec:
import { parseServerSpec } from "@/components/data-grid-server/data-grid-server";
const isIdList = (value: unknown): value is string[] =>
Array.isArray(value) &&
value.length <= 100_000 &&
value.every((id) => typeof id === "string");
export async function POST(request: Request) {
const { ids, spec, exceptIds } = await request.json();
if (isIdList(ids)) return Response.json(await deleteOrders({ ids }));
if (!isIdList(exceptIds)) {
return Response.json({ error: "Bad ids" }, { status: 400 });
}
const bounded = parseServerSpec(spec, { columns: ["customer", "total"] });
return Response.json(await deleteOrders({ spec: bounded, exceptIds }));
}Use the select-all banner
With totalRowCount and checkedRowsLifetime="across-filters", DataGridStatusBar shows a banner:
- When you check every row of a page of a larger result, it reads "All 10 on this page are selected." The button reads "Select all 237 matching" ("Select all matching" for
null). - After the button, it reads "All 237 selected." ("All matching rows are selected." for
null) with the button "Clear selection". Each unchecked row lowers the count by one. - The live region announces the count after each select all matching, check, or uncheck. When the size is unknown, it announces that all matching rows are checked, and later changes announce nothing.
What ends "all matching"
clearSelection(),setSelection(null), and an empty controlledselection- Escape, Ctrl/Cmd+A, and the header checkbox turned off
- A plain click on a row number, or Shift+Space
- In
allMatchingmode, a change of the filter or the search text ends all matching and unchecks every row. The banner starts again from the next checked page. - A row delete:
deleteRowsAt, ordeleteRowswithoutsource: "stream". A server push keeps "all matching". checkedRowsLifetimeset to another value, ortotalRowCountremoved
The grid remembers at most 100,000 ids.
- In
idsmode, it drops the rows beyond the cap. - In
allMatchingmode, more than 100,000 exclusions end "all matching" and uncheck every row, because a dropped exclusion selects more rows. The grid warns once in development. - An unloaded row of a scroll-mode source stays checked in
allMatchingmode, and you cannot exclude it, because it has no id. useDataGridHiddenCheckedCount()is0while "all matching" is on.
Row operations in "all matching"
While "all matching" is on, the Delete rows, Duplicate row, and Move rows shortcuts do nothing, and onBeforeDeleteRows does not run. The Duplicate and Delete context menu items turn off. actions.deleteRows and actions.duplicateRows skip any call whose rows include an unloaded row. Send selected.spec and selected.excludedIds to your own endpoint, then call clearSelection() and refresh().
Copy and export loaded rows
A scroll-mode grid holds unloaded rows as holes. No gesture fetches a row:
- The header checkbox selects all matching when
checkedRowsLifetime="across-filters"holds and the view has unloaded rows. - Ctrl/Cmd+A twice selects the cells of the full view, holes included, and checks no rows.
getRowIdsskips unloaded rows. - Copy writes empty cells for unloaded rows.
- Export covers the loaded rows only and skips holes in every scope. Pass a
sourceto export the whole result. See Export the whole result. - With
totalRowCountanduntil-clickoruntil-cleared,useDataGridSelectedRows()lists real ids only.
Related
- Checked rows: check rows with the row markers
- Selection model: how the grid stores a selection
- Change server rows: insert, duplicate, delete, and move rows
- Load rows while scrolling: how blocks load