Pinned rows
Pin targets or last year's rows to the top and bottom of the table with pinnedTopRows / pinnedBottomRows (display only, a separate array from the body)
Pass arrays of rows to pinnedTopRows / pinnedBottomRows to pin rows right below the column headers and at the bottom of the table. Use them for rows you want to compare with the body, such as targets, last year's results or averages. They don't move when you scroll vertically, and follow horizontal scrolling and the left and right pinned columns like any other row. Used together with the total row, bottom pinned rows sit right above the total row (they can go below it too; see "Placing them below the total row" below).
Demo (branch sales > target on top, last year and average at the bottom)
- "Target" is pinned to the top, and "Last year" and "Branch average" to the bottom. They stay below the column headers and above the total row when you scroll vertically.
- Edit a month's number, and "Branch average" (a bottom pinned row) and the total row follow. Pinned rows are display only, so the average is rebuilt from the body rows and passed in.
- Cells below the target are in red text (
cellClassName). The same rule applies to the pinned "Last year" and "Branch average" rows too. - The bold text in the "Branch" column is put only on pinned rows with
ctx.pinnedinrenderCell. - Pressing a pinned row doesn't move the selection, and it isn't a target of editing, paste or copy.
- Check "Place below the total row" to put the bottom pinned rows below the total row (
pinnedBottomRowsPlacement="belowTotal", since 1.2.0).
Usage
const target: Sale = { id: 'target', branch: 'Target', apr: 1200, may: 1250, jun: 1300, jul: 1350 };
const lastYear: Sale = { id: 'last-year', branch: 'Last year', apr: 1020, may: 1080, jun: 1150, jul: 1190 };
<SpreadsheetGrid
rows={rows}
columns={columns}
rowKeyGetter={(row) => row.id}
pinnedTopRows={[target]}
pinnedBottomRows={[lastYear]}
/>Pinned rows are a separate array from the body rows (rows). They aren't part of the body's count, sorting, filtering or row numbers. The row type is the same T as the body, and the column definitions are used as is.
How they're drawn
Cells are drawn with the same rules as body rows.
- The column's
valueFormatter/renderCell/cellClassNameandgetRowClassName. - Columns with
editor: { type: 'checkbox' }show a read-only mark. - Conditional formatting colors and chips (min / max are decided by the body rows).
To change the look of pinned rows only, tell them apart with pinned in ctx ('top' / 'bottom'; undefined for body rows).
const columns: GridColumn<Sale>[] = [
{
key: 'branch',
title: 'Branch',
renderCell: (ctx) => (ctx.pinned ? <strong>{String(ctx.value)}</strong> : String(ctx.value)),
},
// …
];
<SpreadsheetGrid
getRowClassName={(_row, _index, ctx) => (ctx.pinned === 'top' ? 'target-row' : undefined)}
// …
/>- In a pinned row's ctx,
rowIndex/sourceRowIndexare the position among the pinned rows (from 0),isActive/isSelected/isEditingarefalse, andreadOnlyistrue.setValueinrenderCelldoes nothing. Even with detail rows (detailRow), ctx has nodetail(pinned rows don't expand). - Validation marks (
validate) and notes (cellNote) aren't shown. Cells of the row drag handle, the detail toggle and the auto group column are empty. - The row height is
rowHeight(autoHeightcolumns don't grow either and are cut to one line). The row header shows a pin mark.
Display-only rows
Pinned rows are display only.
- They aren't targets of cell selection, editing, paste, copy, clear, row selection, the context menu, row hover, find in cells (
find) or the changed-value flash (highlightChanges). - They aren't counted in the row count, view row indexes,
getRowNumber, selection aggregates or the total row's aggregates. - To change values, pass a newly built array ("Branch average" in the demo above is built from the body rows with
useMemo).
Position and scrolling
- Top pinned rows stay below the column headers and bottom pinned rows at the bottom of the table (right above the total row if there is one), and body rows flow between them. With few rows, bottom pinned rows come right after the last row.
- Showing rows with the keyboard,
scrollToRoworscrollToCellnever hides them behind pinned rows (rows shown upward go below the top pinned rows, rows shown downward above the bottom pinned rows). - Moving the pointer over pinned rows while dragging a range scrolls in that direction automatically.
Placing them below the total row
Rows computed on from the total row, like subtotal → tax → total with tax, go below the total row with pinnedBottomRowsPlacement="belowTotal" (since 1.2.0; the default 'aboveTotal' is right above the total row).
const subtotal = useMemo(() => rows.reduce((sum, row) => sum + row.amount, 0), [rows]);
const tax = Math.floor(subtotal * 0.1);
<SpreadsheetGrid
rows={rows}
columns={columns}
totalRow={{ label: 'Subtotal' }}
pinnedBottomRows={[
{ id: 'tax', item: 'Tax (10%)', amount: tax },
{ id: 'total', item: 'Total with tax', amount: subtotal + tax },
]}
pinnedBottomRowsPlacement="belowTotal"
/>- Either way they stay at the bottom of the table, and with few rows they come right after the last row.
- The row order in exports (
includePinnedRows+includeTotalRow) and the reading order are the same as on screen. - Without a total row it does nothing.
Export
Not included by default. Pass includePinnedRows: true to exportCsv / getExportData to output top pinned rows before the body rows and bottom pinned rows after them (before the total row; after it with pinnedBottomRowsPlacement="belowTotal"). They aren't output for a selected range (scope 'selection').
grid.current?.exportCsv({ includePinnedRows: true, includeTotalRow: true });
const data = grid.current?.getExportData({ includePinnedRows: true });
data?.rowKinds; // ['pinned', 'data', 'data', …, 'pinned']Look
| Target | Class / attribute / token |
|---|---|
| Top and bottom containers | .ssg-pinned-rows (.ssg-pinned-rows--top / .ssg-pinned-rows--bottom) |
| Rows | .ssg-pinned-row (data-ssg-pinned="top" / "bottom") |
| Cells | .ssg-body-cell.ssg-pinned-cell |
| Background / line between them and the body | --ssg-pinned-row-bg / --ssg-pinned-row-border |
.my-grid {
--ssg-pinned-row-bg: #fff8e6;
--ssg-pinned-row-border: #d9a400;
}With a design system theme, the background is the theme's subdued surface color and the line is the strong border color.
Other notes
- Accessibility: read as table rows (
role="row") and counted inaria-rowcount. Top pinned rows come after the header rows, and bottom pinned rows after the data rows (the order with the total row is the same as on screen). The row header names are 「上に固定 1」 / 「下に固定 1」, changeable withpinnedTopRow/pinnedBottomRowinlocaleText("Pinned to top 1" in the English textslocaleTextEn). - Auto-sizing columns: makes room for pinned rows' values too.
- serverSide (
dataSource): works as is. Pinned rows are drawn from the arrays you pass, so they aren't fetched from the server again. - Next.js server components: pinned rows are plain row data, so you can build them in a server component and pass them as props (when the values contain no functions). The HTML rendered on the server includes the pinned rows too.
/lite: the feature part ispinnedRowsFeature(the Solid version imports it from the main entry).
import { createSpreadsheetGrid, pinnedRowsFeature } from '@ishibashi0112/spreadsheet-grid/lite';
const Grid = createSpreadsheetGrid({ features: { pinnedRows: pinnedRowsFeature } });API details are in the "Pinned rows" section of Props.