Column sizing
Set column widths, let users resize by drag, fit columns to their content with autosize, and let flex columns fill free space.
A column needs a width that fits its data. Set width, minWidth, and maxWidth on the column. The user drags the header edge to resize it. autosizeColumns fits columns to their content. flex shares the free width of the grid.
Resize a header edge, drag a header, or pin/hide a column (header menu / columns menu) — every change persists and is restored on the next reload. Current order: (defaults or saved)
The Column layout example shows resize, reorder, and a saved layout together.
Resize
Drag the end edge of a header to resize the column. If the user selected several columns, the drag still resizes only that one column. Escape during a drag restores the width and fires no onColumnLayoutChange. A double-click on the edge fits the column to its content. A column without width is 150 px wide.
A resize stops at the larger of minWidth and 32 px, and at maxWidth on the other side. The edge handle is wider on a touch screen. On a touch screen, the header leaves room for the edge handle. The menu button and the drag handle of the column stay clear of it.
resizable: false on a column and enableColumnResize={false} on the grid turn the handle off. Both also leave the column out of autosize. This sample sets widths and turns the handle off for the age column:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string; email: string; age: number };
const rows: Row[] = [{ id: "1", name: "Ada", email: "ada@mail.test", age: 36 }];
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name", width: 160, minWidth: 80 },
{
id: "email",
header: "Email",
accessorKey: "email",
width: 220,
maxWidth: 400,
},
{
id: "age",
header: "Age",
accessorKey: "age",
type: "number",
resizable: false,
},
]);
export function SizedGrid() {
return <DataGrid className="h-80" defaultData={rows} columns={columns} />;
}During a drag, onColumnResizing({ columnId, width }) fires on every pointer move. On release of the handle, onColumnLayoutChange fires once.
A drag that ends in a pointer cancel, a lost pointer capture, or a window blur restores the width it started at. If the width moved, onColumnResizing fires once with that width. onColumnLayoutChange does not fire.
Widen column and Narrow column in the header menu change the width by 16 px, within minWidth and maxWidth. Two keymap actions do the same for the active column, and both are unbound by default. This chord pair has no default binding in the grid:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string };
const rows: Row[] = [{ id: "1", name: "Ada" }];
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
export function ResizableByKeyGrid() {
return (
<DataGrid
defaultData={rows}
columns={columns}
keymap={{
widenColumn: ["shift+alt+End"],
narrowColumn: ["shift+alt+Home"],
}}
/>
);
}Call the width actions from your own resize controls:
| Action | Writes | Fires |
|---|---|---|
previewColumnWidth(id, width) | a live width, clamped to the column limits | onColumnResizing |
setColumnWidth(id, width) | the width, clamped to the column limits | onColumnLayoutChange once, when the width changed |
resetColumnWidth(id) | nothing: it drops the stored width, and the column takes the width of its definition | onColumnLayoutChange once, when a stored width existed |
Autosize
A double-click on the edge handle fits one column. The header menu of the context menu add-on has the same "Autosize column" item. Its "Autosize all columns" item fits every visible column that can be resized.
Autosize measures the display text of sampleRows rows. The rows are spread evenly across the sorted and filtered view. On a paged grid, the sample covers the shown page only. The pass adds the rows on screen and the last row. It also measures the header text, with room for the sort arrow, the menu button, and the filter button where the column shows them. The cell padding comes on top, and the result stays inside minWidth and maxWidth.
A text longer than 1000 characters counts as 1000 characters. The pass measures the display text of the cell type. It does not measure the output of renderCell.
actions.autosizeColumns(options) runs the pass from code. It writes every changed width at once, fires onColumnLayoutChange once, and returns the measured widths. A pass that changes nothing fires nothing. Render the button below inside DataGridProvider:
import { useDataGridActions } from "@/components/data-grid/data-grid";
function FitNameButton() {
const actions = useDataGridActions();
return (
<button onClick={() => actions.autosizeColumns({ columnIds: ["name"] })}>
Fit name
</button>
);
}The pass skips hidden columns, columns with resizable: false, and unknown ids.
| Option | Type | Default |
|---|---|---|
columnIds | readonly string[] | every visible resizable column |
sampleRows | number | 1000, clamped to 1 to 10,000 |
includeHeader | boolean | true |
autosizeOnMount on DataGrid or DataGridProvider runs one pass once the view has rows. A grid that loads rows later waits for them. Pass true for the defaults, or pass the options above. The pass runs once per mount. A column with a stored width, for example from defaultColumnLayout, keeps it:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string };
const rows: Row[] = [{ id: "1", name: "Ada" }];
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
export function AutosizedGrid() {
return <DataGrid defaultData={rows} columns={columns} autosizeOnMount />;
}Flex fill
A column with flex grows from its width to take free width. Flex columns share the free width in proportion to their flex values, as CSS flex-grow does. A column without flex, or with flex: 0, keeps its width:
import { defineColumns } from "@/components/data-grid/data-grid";
type Row = { id: string; name: string; email: string; age: number };
export const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name", width: 160, flex: 1 },
{ id: "email", header: "Email", accessorKey: "email", width: 200, flex: 2 },
{
id: "age",
header: "Age",
accessorKey: "age",
type: "number",
width: 80,
},
]);Here email grows twice as fast as name, and age stays at 80 px. maxWidth caps the growth, and the other flex columns take the unused share. A flex column never gets narrower than its width.
A flex column that the user resizes keeps the new width and leaves the flex share. An autosize pass stores the width of each column whose width it changes, so it also fixes a flex column. Pass columnIds to leave the flex columns out. resetColumnWidth puts a column back.
Server mode
Widths belong to the browser in every data mode. The grid sends no width to fetchRows or onSave. Save widths with onColumnLayoutChange.
Autosize measures the rows the grid holds. In pages mode, that is the current page. In scroll mode, that is the loaded rows. Rows that are not loaded stay out of the pass, so a later block with longer text does not widen the column.
Related
Add comments to cells
Attach comments to cells with the data-grid-comments add-on. Keep them in the browser, or save them through your own server and share them between clients.
Column order and pinning
Let users drag columns into a new order and pin columns to the start or end edge so they stay in view while the grid scrolls.