Import into a server grid
Send the imported rows to your server from onImport, then refresh the grid, because a grid with a row count cannot append rows.
A grid with a totalRowCount cannot take appended rows: the data exceeds the count, and the next refresh drops them. Without onImport, its import button is disabled. Pass onImport to send the rows to your server instead.
Loading…. Filter or search, then use the download menu: the plain items export this page, "Export all rows" exports the whole result. Import adds the file's rows on the server.
Before you start
- Install
data-grid-io. See Import. - Fetch the rows with
useDataGridServerRows, so thatserver.refresh()exists.
Steps
- Pass
createRowtoDataGridImportButton. It builds the base row of each imported record. - In
onImport, send the rows to your API and wait for the answer. - Call
server.refresh(). - Set
importLimits.maxRowsto your request limit plus 1.maxRowscounts the header row.
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
defineColumns,
} from "@/components/data-grid/data-grid";
import { DataGridImportButton } from "@/components/data-grid-io/data-grid-io";
import { useDataGridServerRows } from "@/components/data-grid-server/data-grid-server";
import { DataGridToolbar } from "@/components/data-grid-toolbar/data-grid-toolbar";
import { api } from "./api";
type Order = { id: string; customer: string; total: number };
const newOrder = (): Order => ({
id: crypto.randomUUID(),
customer: "",
total: 0,
});
const columns = defineColumns<Order>()([
{ id: "customer", header: "Customer", accessorKey: "customer" },
{ id: "total", header: "Total", accessorKey: "total", type: "number" },
]);
export function OrdersGrid() {
const server = useDataGridServerRows<Order>({
defaultPageSize: 25,
fetchRows: async ({ spec, page, pageSize, signal }) => {
const answer = await api.orders({ spec, page, pageSize }, { signal });
return { rows: answer.rows, totalRowCount: answer.total };
},
});
return (
<DataGridProvider {...server.providerProps} columns={columns}>
<DataGridToolbar>
<DataGridImportButton<Order>
importLimits={{ maxRows: 5_001 }}
createRow={newOrder}
onImport={async (rows, { signal }) => {
await api.createOrders(rows, { signal });
server.refresh();
}}
/>
</DataGridToolbar>
<DataGridRoot {...server.rootProps}>
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridProvider>
);
}Result
The dialog stays pending until onImport settles. If it rejects, the dialog stays open and the user can retry. signal aborts when the user clicks Cancel or the dialog closes. A late answer after that changes nothing. When the build cleared cells, the dialog stays open after onImport resolves. ctx.rejected and ctx.flagged list the cells to report to the user.
The server decides where the new rows sit under the current sort. server.refresh() reads the new totalRowCount and the rows in view. Rows keep the ids that createRow gave them. If your server assigns ids, read them from the refreshed rows.
Related
- Import: the dialog.
- Control the import:
onImportErrorand the other import props. - Save edits: the request shape for writes.
- Import options: every import option and type