Pinned columns example
Pin columns to the start or end side, resize columns, autosize on double-click, and hide and restore columns.
The demo pins Name to the start side and Score to the end side. Scroll the grid sideways to see them stay in place. Email cannot resize, and the long Bio column fills the free width.
Drag a header's edge to resize (Email is fixed), double-click an edge to autosize the long Bio column, or drag a header to reorder. Hover a header for its menu (or right-click) to pin, hide, or autosize — the toolbar's columns menu restores anything hidden.
pnpm dlx shadcn@latest add @gridcn/data-grid-pinning-demo
What the demo shows
pin: "start"on Name andpin: "end"on Score pin the columns to the start side and the end side. A pinned column shows a shadow once other columns scroll under it.resizable: falseon Email removes its resize handle. Drag the end edge of another header to resize it.- On a grid wider than its columns,
flex: 1on Bio fills the free width. Double-click the end edge of its header to autosize it to its content. - Hover a header to see the menu button, or right-click the header. The menu pins, unpins, hides, moves, resizes, and autosizes the column. From the keyboard, Shift+Alt+ArrowDown opens the menu of the active cell's column. The demo wires it with
DataGridContextMenuandDataGridHeaderDropdown. - The
DataGridColumnsMenuin the toolbar hides and restores columns.
Pin, resize, and reorder are on for every column. Set pinnable, resizable, or reorderable to false on a column to turn one off. Set enableColumnPinning, enableColumnResize, or enableColumnReorder to false to turn one off for the grid. To pin rows as well, see the pinned rows example.
The column options of the demo, shortened:
import { defineColumns } from "@/components/data-grid/data-grid";
type Person = {
id: string;
name: string;
email: string;
bio: string;
score: number;
};
export const columns = defineColumns<Person>()([
{ id: "name", header: "Name", accessorKey: "name", width: 160, pin: "start" },
{
id: "email",
header: "Email",
accessorKey: "email",
width: 200,
resizable: false,
},
{ id: "bio", header: "Bio", accessorKey: "bio", width: 140, flex: 1 },
{
id: "score",
header: "Score",
accessorKey: "score",
type: "number",
width: 90,
pin: "end",
},
]);Related
- Column order and pinning: the pin rules and the pin indicator
- Column sizing: resize, autosize, and flex fill
- Column visibility: hide and restore columns
- Column order example: move columns by drag or by code
- Column layout example: save all of the above