Pinned rows example
Pin columns to the start and end sides and rows to a top and a bottom band in one grid, with an averages row and a totals row.
One grid uses all four pin zones. Name is pinned to the start side and Score to the end side. An averages row fills the top band and a totals row fills the bottom band. The pinned rows add-on owns the bands.
Scroll the grid both ways: Name and Score stay pinned to their column bands while the middle columns scroll underneath, and the Average/Total rows stay pinned to their row bands while the data rows scroll underneath.
pnpm dlx shadcn@latest add @gridcn/data-grid-pinning-all-sides-demo
What the demo shows
pin: "start"on Name andpin: "end"on Score pin the two columns.useDataGridPinnedRowstakestopRowsandbottomRows. The top band holds the averages row. The bottom band holds the totals row.aggregateRowdeclares both rows. The band computes them from the rows that pass the filter (thescopedefaults to"view").- The totals row sets the Name cell with a function reducer,
(values, rows) => ..., that shows the row count. - The grid has nine columns and 200 rows. Scroll it in both directions.
Wire the zones
Declare each computed row with aggregateRow and pass it to the hook. An inline aggregateRow keeps the band. A function reducer with a new identity reduces again after each render. Declare such a row at module scope to skip that work. The cells option sets the fixed cells by column id:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import {
aggregateRow,
useDataGridPinnedRows,
} from "@/components/data-grid-pinned-rows/data-grid-pinned-rows";
type Row = { id: string; name: string; age: number; score: number };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name", pin: "start" },
{ id: "age", header: "Age", accessorKey: "age", type: "number" },
{
id: "score",
header: "Score",
accessorKey: "score",
type: "number",
pin: "end",
},
]);
const rows: Row[] = [{ id: "1", name: "Ada", age: 36, score: 90 }];
const AVERAGE = aggregateRow<Row>(
{ age: "avg", score: "avg" },
{ cells: { id: "average", name: "Average" } },
);
const TOTAL = aggregateRow<Row>(
{ name: (_values, rows) => `${rows.length} rows`, age: "sum", score: "sum" },
{ cells: { id: "total" } },
);
function PinnedGrid() {
const { plugin } = useDataGridPinnedRows({
topRows: [AVERAGE],
bottomRows: [TOTAL],
});
return <DataGrid defaultData={rows} columns={columns} plugins={[plugin]} />;
}Behavior
- The pinned columns stay at their edges during a horizontal scroll.
- The pinned bands stay under the header and at the bottom edge during a vertical scroll. The data rows scroll beneath them.
- A cell in a pinned column and a pinned band stays fixed on both axes.
- A band casts a soft shadow after data rows scroll beneath it. See scroll shadows.
Related
- Pinned rows: the add-on, its options, and the aggregate reducers
- Pinned columns example: pin columns on their own