Learning path
Define columns
Step 3 of 8
Set column width, pinning, and cell type options, and add a computed column, building on the first grid.
Before you start, render your first grid.
This step adds settings to the columns of that grid. Name stays in place at the start side, Age gets limits, and a computed Group column fills the leftover width.
Edit the columns
Replace columns.ts with this file:
import { defineColumns } from "@/components/data-grid/data-grid";
export type Person = { id: string; name: string; age: number; active: boolean };
export const columns = defineColumns<Person>()([
{
id: "name",
header: "Name",
accessorKey: "name",
type: "text",
width: 200,
pin: "start",
},
{
id: "age",
header: "Age",
accessorKey: "age",
type: "number",
width: 90,
options: { min: 0, max: 120 },
},
{
id: "active",
header: "Active",
accessorKey: "active",
type: "checkbox",
width: 90,
},
{
id: "group",
header: "Group",
accessorFn: (person) => (person.age >= 18 ? "Adult" : "Minor"),
flex: 1,
},
]);Result: the grid shows a fourth column. Group reads Adult or Minor from the age of each row, and a change to Age updates it.
What each setting does
widthsets the width in pixels. The default is 150.flexshares leftover width between columns. A flex column grows from itswidthand never gets narrower.pin: "start"sets the initial pin side. A pinned column stays in place while the others scroll.optionscarries the settings of the cell type. When a user types or pastes a number outsideminandmax, the cell snaps it to the nearest bound.accessorFncomputes the value. A column withaccessorFnand noaccessorKeyorsetValuelocks itself. To make it writable, addsetValue.
The id of a column stays stable and never comes from the header text.
Next: Edit cells.