Row operations example
Insert, duplicate, and delete rows from a toolbar and the context menu, and read the change that each operation reports.
A toolbar and the context menu run the same row operations. A side panel prints the last change.
Select rows (or just click a cell) and use the toolbar, or right-click a row for the context menu. The panel shows the last onDataChange payload.
pnpm dlx shadcn@latest add @gridcn/data-grid-row-ops-demo
What the demo shows
- The toolbar buttons call
insertRows(anchor, 1, "above"),insertRows(anchor, 1, "below"),runShortcut("duplicateRow"), andrunShortcut("deleteRows").runShortcutacts on the checked rows and the rows of a range. With neither, it acts on the active row. - A right-click on a row opens the context menu with Insert, Duplicate, and Delete items. Insert works at the clicked row. Duplicate and Delete act on the rows that the selection covers, or on the clicked row if the selection is empty.
- The
createRowandduplicateRowprops enable Insert and Duplicate. Without them, the menu hides those items. - The Read-only switch sets
readOnly. The toolbar buttons and the menu items that change data turn off. - The side panel shows the
sourceand one line per operation.
Wire the operations
The grid in this sample has a Delete button and the context menu:
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
defineColumns,
useDataGridActiveCell,
useDataGridApi,
useDataGridSelection,
} from "@/components/data-grid/data-grid";
import {
DataGridContextMenu,
} from "@/components/data-grid-context-menu/data-grid-context-menu";
import { Button } from "@/components/ui/button";
type Row = { id: string; name: string };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
const rows: Row[] = [{ id: "1", name: "Ada" }];
function DeleteButton() {
const api = useDataGridApi();
const activeCell = useDataGridActiveCell();
const hasChecked = useDataGridSelection().rows.length > 0;
const deleteTargets = () => api.runShortcut("deleteRows");
return (
<Button
disabled={!hasChecked && !activeCell}
onClick={deleteTargets}
>
Delete
</Button>
);
}
function RowOpsGrid() {
return (
<DataGridProvider
defaultData={rows}
columns={columns}
getRowId={(row) => row.id}
createRow={() => ({ id: crypto.randomUUID(), name: "New row" })}
duplicateRow={(row) => ({ ...row, id: crypto.randomUUID() })}
onDataChange={(_, change) => console.log(change.source, change.ops)}
>
<DeleteButton />
<DataGridContextMenu>
<DataGridRoot>
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridContextMenu>
</DataGridProvider>
);
}The change
Every insert, duplicate, and delete reports one change with source: "row-operation". An insert and a duplicate write insert operations. A delete writes delete operations. Each operation names its row by rowId and carries the row and its data index.
Because the operations name rows by id, history can undo a row operation after a sort or filter. History records "row-operation" changes by default.
Confirm a delete
onBeforeDeleteRows runs before every delete except a push (source: "stream"). In this demo, it opens a dialog for the Delete rows shortcut and for the menu item:
Check some rows, then press Ctrl/Cmd+Shift+Backspace or right-click a cell and choose Delete rows. A dialog asks first. Cancel keeps the rows. Delete removes them.
pnpm dlx shadcn@latest add @gridcn/data-grid-delete-confirm-demo
rowMarkers="checkbox"andcheckedRowsLifetime="until-cleared"make the delete act on the checked rows.onBeforeDeleteRowsreturns a promise. Cancel resolves it withfalseand keeps the rows. Delete resolves it withtrue. A dismissed dialog resolves it withfalse.- The Cancel button gets the focus when the dialog opens.
Related
- Row operations: the factory props, shortcuts, and code actions
- Context menu: which rows the menu items act on
- Row actions example: per-row buttons in a pinned column
- Streaming rows example: batches of cell updates