SpreadsheetGrid
Guides

Label rows

Draw heading rows mixed into rows as full-width bands with labelRow (not counted as rows, sorting within sections, sticky)

Pass the labelRow prop to treat certain rows in rows as "heading (separator) rows". They're the same as section heading rows on an Excel sheet: drawn as a full-width band at the position where they sit between data rows, and not counted as rows (they use no row number and are excluded from editing, selection, copy and export by default). The row type T and the types of rows / onRowsChange don't change, so data imported from Excel or CSV can be used in its own shape.

How it differs from row grouping

They look alike, but this is a different feature from row grouping.

Label rows (labelRow)Row grouping (rowGroup)
What it doesDraws heading rows placed in rows as bandsRegroups rows by column values and creates group rows
OrderThe order of rows as is (sorting stays within sections)Reordered by group
Aggregates / tree / open-closeNoneYes (aggFunc / auto group column / open-close)
Data sourceThe row data itself (identified with isLabelRow)Column definitions (rowGroup: true)
TogetherNot possible (label rows aren't shown while rowGroup is on)

If "the data has separators (per process in a quote, per section in a report)", use label rows; if you "want to regroup by column values and aggregate", use grouping.

Demo (quote lines > headings per process)

Rows 16 of 16Columns 7 of 7Sort: —
#
Code
1
3B0620AEZ6
2
6FA000B357
3
7641205200
4
7641205401
5
GA23930PB1
6
GA23938PS1
7
6DA000B450
8
GA23941PB2
9
6DA006B450
10
6A00380198
11
6FFA000B60
12
6A00380221
13
9C0011A001
14
9C0011A014
15
9C0011A027
16
9C0011A033
StepBelt conveyor (TAC23A0042)4 items
StepPattern feeder (MDST)4 items
StepConveyor roller module base4 items
StepControl panel (CB-2)4 items
Rows 16 of 16Columns 7 of 7
Cell —Range —0 cells / 0 rows0 columns
  • Rows with kind: 'label' are headings. They're drawn as bands across the 3 panes, and the heading stays at the left edge when you scroll horizontally.
  • Scroll vertically, and the current process's heading is pinned right below the column headers; when the next heading arrives, it pushes the pinned one up and takes over (sticky: true).
  • Sorting from a column header doesn't move the headings (sorting happens only within each process). Filter by the "Status" column, and processes left with 0 rows disappear along with their headings.
  • Row numbers and the count ("Rows X of Y" in the bar) don't count headings. ↑ / ↓ don't stop on headings, and paste skips headings and continues.

Usage

type Row = { id: string; kind?: 'label'; code: string; name: string; qty: number };

<SpreadsheetGrid
  rows={rows}                       // mix label rows into the same array as data rows
  columns={columns}
  rowKeyGetter={(row) => row.id}
  labelRow={{
    isLabelRow: (row) => row.kind === 'label',   // required: identifies label rows
    getLabel: (row) => row.name,                 // required: display text (used for export / aria too)
    sticky: true,                                // keep headings below the header while scrolling
    height: 34,                                  // rowHeight when omitted
  }}
/>

Decorating with your own parts (render)

Pass render to make the contents any element (React elements in the React version, Solid JSX in the Solid version). ctx is { row, rowKey, rowIndex, sourceRowIndex, label, sectionRowCount }, where sectionRowCount is the number of data rows in that section (after filtering).

labelRow={{
  isLabelRow: (row) => row.kind === 'label',
  getLabel: (row) => row.name,
  render: ({ label, sectionRowCount }) => (
    <>
      <span className="rounded bg-blue-600 px-1.5 text-xs text-white">Section</span>
      <strong>{label}</strong>
      <span className="text-gray-500">{sectionRowCount} items</span>
    </>
  ),
  className: (row) => (row.important ? 'label-row--important' : undefined),
}}

The default look (without render) is an accent line on the left + bold text, and colors can be adjusted with the tokens --ssg-label-row-bg / --ssg-label-row-text / --ssg-label-row-accent. The part slots are classNames.labelRow (the row element) and classNames.labelRowContent (the contents container).

Different types for data rows and label rows (TypeScript)

When label rows have fields of their own, make the row type a union and write isLabelRow as a type guard; then row in getLabel / render / height / className / exportText becomes the label row type.

type Sale = { kind: 'sale'; id: number; customer: string; amount: number };
type Month = { kind: 'label'; id: string; label: string };
type Row = Sale | Month;

labelRow={{
  isLabelRow: (row): row is Month => row.kind === 'label',
  getLabel: (row) => row.label,            // row is Month
  render: ({ row }) => <strong>{row.label}</strong>,
}}

With TypeScript 5.5 or later, just writing (row) => row.kind === 'label' is inferred as a type guard. When you write type arguments, write two: <SpreadsheetGrid<Row, Month>> (writing only one, <SpreadsheetGrid<Row>>, leaves row as Row even with a type guard, and you get Property 'label' does not exist in getLabel). When you put the options in a variable, use LabelRowOptions<Row, Month>.

Sections and sorting / filtering

From a label row to just before the next label row counts as one section (rows before the first label row are an "unlabeled section"). How sorting and filtering behave is chosen with sortMode.

sortModeBehavior
'section' (default)Sorting happens only within each section, and label rows don't move. Sections left with 0 rows by a filter disappear along with their label (keep them with keepEmptySections: true)
'follow'Sorts everything, and label rows move with "the data row that comes first from their original section", right before it (labels of sections that were empty to begin with line up right before the label of the following section)
'hide'Label rows aren't shown while sorting or filtering is applied

With no sorting and no filter, every mode shows the order of rows as is.

Sticky on vertical scroll (sticky)

With sticky: true, the label row of "the current section" is pinned right below the column headers while scrolling vertically. When the next label row's top edge reaches the bottom of the pinned band, the pinned heading is pushed up and replaced (the same as section headers in a phone's contact list). Pinned headings stay at the left edge when scrolling horizontally too. Showing upper rows with the keyboard (↑ / PageUp / Ctrl+Home and so on) or scrollToRow / scrollToCell places them below the pinned heading (never hidden behind it).

Export

Not included in output by default (not output as data rows with scope 'raw' either). With includeLabelRows: true, headings are output as one row each.

gridRef.current?.downloadCsv('estimate.csv', { includeLabelRows: true });

const { columns, rows, rowKinds } = gridRef.current!.getExportData({ includeLabelRows: true });
// rows where rowKinds[i] === 'label' are headings. Apply merged cells / bold in Excel

Output values can be replaced with exportText (a string goes in the first column, an array is used as is in column order). For a recipe that makes merged cells with ExcelJS, see the API reference.

With other features

  • Row drag: data rows can move across sections (they then belong to the destination section). Label rows themselves can't be grabbed.
  • Detail rows: work together. Label rows can't be expanded.
  • Row grouping: doesn't work together. Label rows aren't shown while rowGroup is on (development warning).
  • serverSide (SSRM): the server returns label rows in the results of getRows (counted in totalRowCount too), identified with isLabelRow. Section sorting, sectionRowCount, sticky and height don't apply with the server-side row model.

For the list of options, see the props reference.

On this page