gridcn2.0.0
Row examples

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.

Name
Email
Age
Role
Joined
Active
Department
Location
Score
Silent Tiger
user0@example.com
63
Viewer
2023-07-05
Sales
Remote
4
Quick Lion
user1@example.com
67
Viewer
2021-04-03
Sales
Onsite
89
Swift Wolf
user2@example.com
40
Manager
2020-10-27
Engineering
Remote
80
Eager Lion
user3@example.com
50
User
2022-10-08
Engineering
Onsite
51
Eager Tiger
user4@example.com
45
Viewer
2024-10-07
Sales
Onsite
83
Silent Wolf
user5@example.com
24
User
2020-06-01
Engineering
Onsite
15
Swift Lion
user6@example.com
34
Viewer
2022-10-18
Engineering
Remote
62
Eager Lion
user7@example.com
21
Manager
2021-10-07
Sales
Remote
42
Eager Eagle
user8@example.com
25
Editor
2024-06-11
Sales
Onsite
35
Bold Tiger
user9@example.com
19
Editor
2024-03-20
Sales
Remote
52
Bold Bear
user10@example.com
42
User
2020-04-16
Engineering
Remote
28
Swift Wolf
user11@example.com
47
Manager
2022-08-04
Sales
Onsite
9
Swift Bear
user12@example.com
41
Viewer
2024-05-04
Sales
Onsite
45
Eager Wolf
user13@example.com
35
Editor
2020-02-16
Sales
Remote
44
Swift Eagle
user14@example.com
65
Viewer
2023-07-13
Sales
Onsite
51
Quick Lion
user15@example.com
22
Manager
2023-03-20
Engineering
Remote
28
Bold Wolf
user16@example.com
30
Editor
2023-08-07
Engineering
Remote
76
Swift Eagle
user17@example.com
19
Admin
2022-03-22
Sales
Remote
26
Eager Eagle
user18@example.com
35
Manager
2020-11-03
Sales
Remote
70
Eager Bear
user19@example.com
33
Editor
2022-09-21
Sales
Onsite
67
Silent Lion
user20@example.com
48
Manager
2021-01-03
Engineering
Onsite
89
Silent Tiger
user21@example.com
62
Manager
2024-01-12
Engineering
Remote
40
Eager Tiger
user22@example.com
51
Manager
2023-08-01
Sales
Remote
82
Silent Lion
user23@example.com
26
Admin
2020-09-03
Engineering
Remote
16
Bold Eagle
user24@example.com
26
Admin
2024-09-10
Engineering
Remote
38
Bold Lion
user25@example.com
49
Viewer
2022-01-02
Sales
Onsite
3
Quick Tiger
user26@example.com
26
Viewer
2021-02-08
Engineering
Onsite
41
Bold Eagle
user27@example.com
50
Editor
2024-02-20
Engineering
Remote
10
Swift Tiger
user28@example.com
55
Editor
2023-12-02
Sales
Remote
6
Silent Lion
user29@example.com
35
Viewer
2020-04-17
Sales
Remote
42
Average
42.79
46.73
200 rows
8558
9346
pnpm dlx shadcn@latest add @gridcn/data-grid-pinning-all-sides-demo

What the demo shows

  • pin: "start" on Name and pin: "end" on Score pin the two columns.
  • useDataGridPinnedRows takes topRows and bottomRows. The top band holds the averages row. The bottom band holds the totals row.
  • aggregateRow declares both rows. The band computes them from the rows that pass the filter (the scope defaults 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.

On this page