SpreadsheetGrid
Guides

Row grouping

Hierarchical grouping and aggregate rows with rowGroup / aggFunc

Columns with column.rowGroup: true group the view rows (with several, the order of appearance in the columns array is the hierarchy order). Columns with aggFunc show aggregate values in group rows. Hierarchies where the parent rows are data rows too (departments and sections, folders and files) are tree data.

Demo (two levels: Region > Rep)

Rows 9 of 9Columns 3 of 5Sort: —
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Rows 9 of 9Columns 3 of 5
Cell —Range —0 cells / 0 rows0 columns
  • Open and close with the group row's chevron (or double-click / Enter / Space). Pressing a cell of a group row makes it the active cell, so you can also press it and then Enter / Space to open or close.
  • The Qty / Amount columns show aggFunc: 'sum' aggregates. valueFormatter also applies to aggregate values, so they match the leaf cells.
  • "Expand all / Collapse all" are the imperative handle's expandAllGroups() / collapseAllGroups().
  • "Group by this column" in a column's ⋮ adds that column to the bottom of the hierarchy (Product's ⋮ gives Region > Rep > Product). While grouped, "Ungroup" removes all levels. Both only in tables that receive column definitions through onColumnsChange. The items appear not only on columns with rowGroup but on every column except aggregate columns and the last data column (there's no setting to hide them per column). "Reset columns" also returns grouping to the initial column definitions.
  • "Sort descending" in Amount's ⋮ orders the groups by largest total as well.
  • Press "Filter" in the top bar and choose "Region (grouped)" from "+ Add filter…", and the Region filter opens below the auto group column's header (group source columns can be filtered even though they're hidden).

Column definitions

const columns: GridColumn<Row>[] = [
  { key: 'region', title: 'Region', width: 100, rowGroup: true },
  { key: 'rep', title: 'Rep', width: 100, rowGroup: true },
  { key: 'product', title: 'Product', width: 170 },
  { key: 'qty', title: 'Qty', width: 100, aggFunc: 'sum' },
  { key: 'amount', title: 'Amount', width: 140, aggFunc: 'sum',
    valueFormatter: numberFormatter() },
]

Key points

  • Auto group column — when grouping is on, a tree column (indent + toggle + label + count) is injected automatically at the start, and the group source columns leave the display.
  • Built-in aggregates — 'sum' | 'min' | 'max' | 'avg' | 'count'. Only values that can be read as numbers count, and empty values are excluded (count is the number of leaf rows under it). For custom aggregates, pass a function (GridAggFunc<T>).
  • How aggregates are displayed — the same format as the total row. The column's valueFormatter is applied to aggregates if present; otherwise numbers get thousands separators with up to 4 decimals (the same for numbers from custom aggregates; strings stay as they are). Aggregates other than sum (average / min / max / count) show the kind in small text before the value (Avg 4,115.33). When it doesn't fit the column width, the end becomes "…", and hovering shows the full text. Averages tend to have long decimals and get cut off as "Avg 4,9…" in a column of about 100px with numberFormatter() (up to 3 decimals by default), so fix the digits with numberFormatter({ maximumFractionDigits: 0 }) or the like, or widen the column.
  • With filtering / sorting — filters apply to leaf rows, and groups left with 0 rows disappear. Groups are ordered by "first appearance after sorting". Sorting by an aggregate column (aggFunc) also orders the groups by that aggregate (descending Amount puts the largest total on top; if that level's group source column comes first in the sort conditions, that column's order). Group source columns leave the display, but sorting and column filters on them still work (sorting by a group source column reorders whole groups; the "Manage sorting…" panel offers group source columns. To filter a group source column, choose it as "Region (grouped)" from "+ Add filter…" in the filter management panel, and it opens below the auto group column's header; applied filters appear in the management panel and chip bar as "(grouped)"). The global filter also searches the values of group source columns. The "Columns" tab of the tool panel shows group source columns as "(grouped)", and their visibility can't be toggled (dragging to reorder swaps the hierarchy order too).
  • Features for leaves only — group rows aren't covered by editing / copy / row selection / export (all apply to leaf rows). Paste skips group rows and continues into the next leaf row. Export outputs all leaf rows after filtering / sorting regardless of open state (including rows under collapsed groups).
  • Heading text (groupLabel) — group row headings are the value text. For columns whose values are codes ('chubu') and whose screen shows display names ("Central"), groupLabel: 'formatted' uses the display name from the column's valueFormatter (a function uses its return value. Since 1.1.0). To search display names with the global filter too, use globalFilterMatch="both" (Values and labels).
  • clientSide only — with serverSide (dataSource), rowGroup is ignored.
  • Saving state — grouping (rowGroup) and open state go into GridState with includeGroupingInState, and come back through restoreColumnsFromState / initialState / applyState (since 1.2.0; Saving and restoring state). Without it they're not included, and open state returns to the default (all open) on every mount.
  • Find in cells (find) — only displayed rows are searched. Rows inside collapsed groups and values of hidden group source columns aren't searched (the global filter can narrow by them).

Export (CSV / TSV / Excel)

While grouped, exportCsv / downloadCsv / getExportData put the group source columns that left the display at the start. Every row carries its group values, giving a flat table you can use as is for Excel pivots or reloading. With includeGroupRows: true, a subtotal row (each aggregate column holds that group's aggregate) goes at the start of each group too (for reports; only with scope 'view').

gridRef.current?.exportCsv();
// Region,Segment,Rep,Amount
// East,Corporate,Sato,100
// East,Consumer,Suzuki,200

gridRef.current?.exportCsv({ includeGroupRows: true });
// Region,Segment,Rep,Amount
// East subtotal,,,300
// East,Corporate subtotal,,100
// East,Corporate,Sato,100
// East,Consumer subtotal,,200
// East,Consumer,Suzuki,200

With getExportData({ includeGroupRows: true }), subtotal rows can be told apart by 'group' in rowKinds (to make them bold in Excel and so on). Aggregate values are raw values (numbers stay numbers). Rows excluded from output by isRowExportable aren't counted in subtotals, and groups with no rows left don't get a subtotal row. The total row (includeTotalRow) has the same values as the total row on screen, so it counts excluded rows too (it may not match the sum of the subtotals).

For the imperative open / close API (setGroupCollapsed / getGroupRows and so on), see the API reference.

On this page