gridcn2.0.0
Save to a server

Undo with server data

Save undo and redo to your server, drop the undo entry of a refused save, and run the history add-on over scroll-mode rows.

The history add-on records the changes a user makes. When the grid saves to a server, an undo is one more change to save. This page shows how undo and redo reach onSave, how a refused save leaves the undo stack, and how to run history over scroll-mode rows.

Save undo and redo

An undo or redo reaches onDataChange and onSave with source: "history". Your onSave sees it as a normal change and sends it to the server. Do not filter that source out, or the server keeps the old value.

Roll back a failed save

If the server refuses a change, return the refusal from onSave. The grid reverts the change and, when the revert writes, removes its entry from the undo stack, so no dead Ctrl+Z step remains. The whole entry goes, so the rows that the server accepted lose their undo step. See Handle refused saves.

In this demo, the mock server refuses a short name, and the grid reverts the cell:

Edit a name. Type "Al" and press Enter: after the delay the server refuses it and the cell goes back.

Name (3+ characters)
City
Person 0
Hamburg
Person 1
Berlin
Person 2
Hamburg
Person 3
Berlin
Person 4
Hamburg
Person 5
Berlin
Person 6
Hamburg
Person 7
Berlin
Person 8
Hamburg
Person 9
Berlin
Person 10
Hamburg
Person 11
Berlin
All changes saved

To keep the user's row or merge it with the server row, see Resolve a conflict.

A rejected promise or a { rejected } answer reverts for you. Call rollback(change) with the change that onSave received only when your handler catches the failure and does not rethrow:

import {
  DataGrid,
  type DataChange,
} from "@/components/data-grid/data-grid";
import { useDataGridHistory } from "@/components/data-grid-history/data-grid-history";
import { columns, type Row } from "./rows";
import { api } from "./api";

export function RowsGrid({ rows }: { rows: Row[] }) {
  const history = useDataGridHistory<Row>();

  async function onSave(change: DataChange<Row>) {
    try {
      await api.save(change);
    } catch {
      history.rollback(change);
    }
  }

  return (
    <DataGrid
      defaultData={rows}
      columns={columns}
      getRowId={(row) => row.id}
      onSave={onSave}
      plugins={[history.plugin]}
    />
  );
}

rollback applies the inverse change with source: "rollback" and removes the entry from the stack. A cell reverts only while it still holds the change's value, so later edits stay. If the user already undid the change, rollback writes nothing. onSave never receives a "rollback" change, and history never records a "history", "rollback", "merge", or "id-swap" change. createHistory from @/components/data-grid/data-grid-sdk exposes the same removal as discard(change).

If the server refuses an undo or a redo, the grid reverts the data and, when the revert writes, puts the entry back. A refused undo is on the undo stack again, and a refused redo is on the redo stack again. A change that took server content (serverRowIds) removes the older operations on those rows from the stacks. Undo and redo lists the rules.

Run history with scroll mode

useDataGridServerRows in scroll mode owns the data array. The array is sparse. Loaded indices hold rows, and unloaded indices hold holes. Pass the history plugin in the plugins option of the hook, which mounts it after its own plugins. datasetKey: serverSpecKey clears the stacks when the sort, the filters, or the search text change:

import { DataGrid, serverSpecKey } from "@/components/data-grid/data-grid";
import { useDataGridHistory } from "@/components/data-grid-history/data-grid-history";
import { useDataGridServerRows } from "@/components/data-grid-server/data-grid-server";
import { columns, type Row } from "./rows";
import { fetchRows } from "./api";

export function ScrollGrid() {
  const history = useDataGridHistory<Row>({ datasetKey: serverSpecKey });
  const server = useDataGridServerRows<Row>({
    mode: "scroll",
    fetchRows,
    plugins: [history.plugin],
  });

  return <DataGrid {...server.gridProps} columns={columns} />;
}

These rules keep the combination safe:

  • Pass a custom getRowId to the hook, never to the grid, and pass server.gridProps to the grid. The grid runs getRowId on every element, holes included, and a plain (row) => row.id throws on a hole. The hook's own option sees loaded rows only, and gridProps.getRowId gives each hole a placeholder id.
  • An entry whose rows left the loaded rows applies nothing, stays on its stack, and calls onSkip with "rows-not-loaded". Load the rows, then undo again.
  • server.refresh() drops every loaded row outside the rendered rows. A change of spec starts the blocks over. A row operation in a source with a row count runs that refresh.
  • A refresh, a row operation, and a tick of revalidateIntervalMs refetch the same result, so the stacks stay. An undo writes only the cells of its entry onto the refetched row, so a value that the server changed in another column stays.
  • datasetKey clears the stacks when it changes. Pass a key that changes with the result, as serverSpecKey does for the spec. Without a key, call clear() in the handler that loads a new result.
  • Call rollback(change) only when your onSave catches a failure and does not rethrow. A rejected promise reverts for you.

When undo needs a row that a hole hides, it warns once in development. An undo whose rows are all loaded does not warn.

Run history with pages mode

Pages mode holds one page in data. The same rules apply, with these differences:

  • A page change leaves entries whose rows are not in data. Such an entry applies nothing, stays on its stack, and calls onSkip with "rows-not-loaded". Go back to that page, then undo again.
  • Pass datasetKey: serverSpecKey here too. A new sort, filter, or search text clears the stacks.

On this page