Migrate to 2.0
Move a grid from 1.3 to 2.0. Replace the lazy rows add-on with server rows, apply each breaking change, and check the changed behavior.
Follow these steps to move a grid from gridcn 1.3 to 2.0. 2.0 breaking changes lists each change with its 1.3 form, its 2.0 form, and the change to make.
Upgrade to 1.3.0 and commit your work first. For an older version, apply the Breaking: entries up to 1.3.0, as Upgrade gridcn shows.
Steps
- Reinstall the core and each add-on that you use, as Upgrade gridcn shows. Leave out
data-grid-lazy, because 2.0 has no such item. If you used it, installdata-grid-serverinstead. - If you used
data-grid-lazy, move each grid touseDataGridServerRows, as Server rows shows. Then delete thecomponents/data-grid-lazy/folder. A reinstall does not remove it. - Run
npx tsc --noEmit. Each error names a removed or renamed identifier. Find the identifier on 2.0 breaking changes and apply its row. - Search your CSS and selectors for
data-pinned="left",data-grid-pin-shadow,data-scrolled-left, and--grid-pin-shadow-left-x, and theirrightforms. Rename them as the pin rows show. The type check does not see these names. - Check the behavior changes, which compile without an error. The type check does not report them:
- Sorting and filtering: the sort language, the text order, the callback timing, the empty filtered grid, and the set operators.
- Specs: the unknown column spec and the equal spec.
- Selection: the checkbox state, row actions over unloaded rows, and the row move under a server sort.
- Row actions: the row ids that
deleteRowstakes. In JavaScript, an old index does nothing. - Editing: Delete and Cut, number text, select text, bulk writes on "warn" columns, and validate rules.
- Clipboard and undo: the paste and cut results, the cut refusal, the cut source, clipboard formulas, and undo on a read-only grid.
- Columns: the def
pinandhidden, the cellz-index, the columnlocale, andsetColumnWidth. - Keys: the default chords and the global shortcut flags, and the bare-key bindings.
- Server data: stream writes, the stream batch size, the request delay, the loaded row cap, the URL parts, and the URL page size.
- Import and export: import on a read-only grid, the delimiter detection, the XLSX part cap, the XLSX cell types, and the dropzone caption.
- Add-ons: find and replace, the
undefinedlabel, and the right-click on a blank area.
- Run your tests.
The type check and the tests pass. Each grid sorts, checks rows, and writes data as your code expects.
Server rows
useDataGridServerRows({ mode: "scroll" }) from data-grid-server replaces useDataGridLazyRows. DataGridLazyGuard has no replacement. If a client sort or filter changes over unloaded rows, the core warns in development.
| 1.3 | 2.0 |
|---|---|
fetchRows(start, end, signal) resolves the rows | fetchRows({ start, end, signal }) resolves { rows, totalRowCount?, hasMore?, nextCursor? } |
total | totalRowCount in the answer |
batchSize | blockSize |
reset(), isLoading | refresh(), loading |
Range from data-grid-lazy | RowRange from data-grid |
maxFetchRows | none (one block for each request) |
onError(error, range) | onError(error) |
Remove your onRenderedRowsChange and onDataChange wiring, and spread server.gridProps:
"use client";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import {
useDataGridServerRows,
} from "@/components/data-grid-server/data-grid-server";
type Order = { id: string; customer: string };
const columns = defineColumns<Order>()([
{ id: "customer", header: "Customer", accessorKey: "customer" },
]);
export function OrdersGrid() {
const server = useDataGridServerRows<Order>({
mode: "scroll",
blockSize: 100,
fetchRows: async ({ start, end, signal }) => {
const query = `offset=${start}&limit=${end - start}`;
const response = await fetch(`/api/orders?${query}`, { signal });
const answer = await response.json();
return { rows: answer.rows, totalRowCount: answer.total };
},
});
return <DataGrid {...server.gridProps} columns={columns} className="h-96" />;
}Scroll mode starts a block request after loadDelayMs milliseconds (default 100). Pass loadDelayMs: 0 for the 1.3 timing. It keeps at most maxLoadedRows rows (default 10,000). Infinity keeps every row.
Related
- 2.0 breaking changes: each change with its 1.3 and 2.0 form
- Upgrade gridcn: reinstall the files and keep your edits
- Version policy and changelog
- Load rows while scrolling