gridcn2.0.0
Row examples

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.

Read-only
Name
Role
Age
Silent Tiger
Viewer
63
Quick Lion
Viewer
67
Swift Wolf
Manager
40
Eager Lion
User
50
Eager Tiger
Viewer
45
Silent Wolf
User
24
Swift Lion
Viewer
34
Eager Lion
Manager
21
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"), and runShortcut("deleteRows"). runShortcut acts 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 createRow and duplicateRow props 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 source and 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.

Row markers
Name
Role
Score
Silent Tiger
Viewer
4
Quick Lion
Viewer
89
Swift Wolf
Manager
80
Eager Lion
User
51
Eager Tiger
Viewer
83
Silent Wolf
User
15
Swift Lion
Viewer
62
Eager Lion
Manager
42
Eager Eagle
Editor
35
Bold Tiger
Editor
52
Bold Bear
User
28
Swift Wolf
Manager
9
Swift Bear
Viewer
45
Eager Wolf
Editor
44
Swift Eagle
Viewer
51
Quick Lion
Manager
28
Bold Wolf
Editor
76
Swift Eagle
Admin
26
Eager Eagle
Manager
70
Eager Bear
Editor
67
pnpm dlx shadcn@latest add @gridcn/data-grid-delete-confirm-demo
  • rowMarkers="checkbox" and checkedRowsLifetime="until-cleared" make the delete act on the checked rows.
  • onBeforeDeleteRows returns a promise. Cancel resolves it with false and keeps the rows. Delete resolves it with true. A dismissed dialog resolves it with false.
  • The Cancel button gets the focus when the dialog opens.

On this page