Row markers
Add a row-number, checkbox, or drag-handle column at the start of the grid, and replace its cells and its header with your own content.
Users need a place to grab a whole row. The marker column sits at the start of the grid and shows a row number, a checkbox, a drag handle, or a mix. It is not one of your columns. Column reorder and resize do not affect it, and it has no column index. Its width depends on the mode.
Pick "both" and hover a row: the row number is replaced by a checkbox while hovered or selected.
Choose a mode
Set rowMarkers on the grid. The default is "none", which renders no marker column. This sample sets "both", which shows the row number and a checkbox:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
const rows: Row[] = [{ id: "1", name: "Ada" }];
function MyGrid() {
return <DataGrid defaultData={rows} columns={columns} rowMarkers="both" />;
}| Mode | Shows | Width |
|---|---|---|
"none" | No marker column. | none |
"number" | The 1-based view row index. | 44 px |
"checkbox" | A row checkbox, and a select-all checkbox in the header. | 36 px |
"both" | The row number. A checkbox replaces it on hover and on a selected row. | 56 px |
"reorder" | A drag handle in the whole cell. | 32 px |
"reorder-number" | The drag handle, then the number. | 56 px |
"reorder-checkbox" | The drag handle, then the checkbox. | 48 px |
"reorder-both" | The drag handle and the content of "both". | 64 px |
| Prop | Type | Default |
|---|---|---|
rowMarkers | RowMarkersMode | "none" |
enableRowSelection | boolean | true |
Selecting rows
A press on a marker cell selects its row. Press and drag to select a range of rows. Shift extends the range, and Ctrl/Cmd adds a row. A press on the checkbox toggles that row and keeps the other checks. The select-all checkbox in the header shows whether the rows are checked: checked, indeterminate, or unchecked.
The checkboxes are not tab stops. Use Shift+Space to select the active row and Ctrl+Shift+Space to toggle it. enableRowSelection={false} turns off the marker gestures and Shift+Space. See Pointer selection and Checked rows.
The reorder family adds a drag handle to the marker cell. Only the handle starts a reorder. The number and the checkbox keep selecting rows. See Row order.
The built-in checkboxes take their aria-labels from labels.markers.selectRow and labels.markers.selectAll.
Custom markers
DataGridRoot and DataGrid accept two render props. They replace the content of the marker column and keep its chrome and its selection gestures:
| Prop | Type | Default |
|---|---|---|
renderMarker | MarkerCellRenderer | built-in number and checkbox |
renderMarkerHeader | MarkerHeaderRenderer | built-in select-all checkbox |
markerWidth | number | width of the mode |
renderMarker receives rowId, the 0-based viewRowIndex, and two flags. If the row is checked, isChecked is true. If the selection covers at least one cell of the row, isCellSelected is true. A cell click sets the second flag and never sets the first. With 'until-click' (the default for markers without a checkbox), a cell click also clears the checked rows.
In a mode of the reorder family, renderMarker also receives reorderHandleProps. Spread it onto an element of the marker to make that element the drag handle. If the renderer never reads reorderHandleProps, the grid adds its own drag handle next to the custom content. renderMarkerHeader receives allSelected, which is "checked", "indeterminate", or "unchecked". It also receives ariaLabel, the name of the select-all control, and toggleAll(), which checks every row or clears the checked rows. In modes without a checkbox, the header is blank, so the renderer fills the blank cell.
The mode sets the width in px. markerWidth overrides it, for a wide row number or wide custom content. With "none", the grid renders no marker column and calls neither function. In the other modes, a press or drag on a custom marker cell still selects rows. In "reorder-number", "reorder-checkbox", and "reorder-both", renderMarker replaces the drag handle too, so a marker without reorderHandleProps has no handle. In "reorder", the whole cell stays the drag handle zone. Define both functions at module scope, because a new function on every render re-renders every marker cell:
import { Check, Circle } from "lucide-react";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import type { MarkerCellRenderer } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
const rows: Row[] = [{ id: "1", name: "Ada" }];
const renderMarker: MarkerCellRenderer = ({ isChecked }) =>
isChecked ? (
<Check className="size-3.5 text-muted-foreground" aria-hidden="true" />
) : (
<Circle className="size-3.5 text-muted-foreground" aria-hidden="true" />
);
function MyGrid() {
return (
<DataGrid
defaultData={rows}
columns={columns}
rowMarkers="number"
renderMarker={renderMarker}
/>
);
}Interactive content in a custom marker needs its own accessible name. The grid supplies the labels.markers names only for its built-in controls.
Server mode
The number is the position in the view. It is not a server row number, so it restarts at 1 on each page of a paginated grid. For select-all over a whole server result, see Select server rows.
Related
- Row order: the drag handle zone and the move keys
- Checked rows: how checks survive filters and pages
- Pointer selection: the marker press and drag gestures
- Labels: translate the marker names
- Row markers example: custom marker and select-all renderers