SpreadsheetGrid
Guides

Total row

Pin a total row to the bottom of the table with totalRow (per-column aggregates with aggFunc, recomputed with filters, display only)

Pass the totalRow prop to pin a total row to the bottom of the table. It's the same as Excel's "Total Row": it stays at the bottom when you scroll vertically, and follows horizontal scrolling and the left and right pinned columns like any other row. Which columns to aggregate, and how, is decided by each column's aggFunc (the same built-in aggregates and custom functions as row grouping), and with a filter applied it recomputes from the visible rows only.

Demo (order lines > total row at the bottom)

Rows 120 of 120Columns 8 of 8Sort: —
#
Order no.
1
SO-24001
2
SO-24002
3
SO-24003
4
SO-24004
5
SO-24005
6
SO-24006
7
SO-24007
8
SO-24008
9
SO-24009
10
SO-24010
11
SO-24011
12
SO-24012
13
SO-24013
14
SO-24014
15
SO-24015
16
SO-24016
17
SO-24017
18
SO-24018
19
SO-24019
20
SO-24020
21
SO-24021
Total
Amount
570
25,600
25,200
24,000
2,000
20,520
129,500
400
5,130
12,800
92,400
14,400
400
13,680
55,500
2,000
2,850
38,400
58,800
4,800
3,600
3,720,450
Rows 120 of 120Columns 8 of 8
Cell —Range —0 cells / 0 rows0 columns
  • Item = count, Qty = sum, Unit price = average, Amount = sum. Aggregates other than sum show the kind (Avg / Count) in small text before the value.
  • Filter by the "Type" column, and the total row recomputes from the rows of that type only. It follows when you edit a quantity too.
  • The total row follows the left-pinned "Order no." and the right-pinned "Amount" too, and stays aligned with the columns when you scroll horizontally.
  • The total row is display only. Pressing it doesn't move the selection, and it isn't a target of editing, paste or copy.

Usage

const columns: GridColumn<Order>[] = [
  { key: 'product', title: 'Product', width: 160, aggFunc: 'count' },
  { key: 'qty', title: 'Qty', width: 90, align: 'right', aggFunc: 'sum' },
  { key: 'price', title: 'Unit price', width: 110, align: 'right', aggFunc: 'avg', valueFormatter: yen },
  { key: 'amount', title: 'Amount', width: 120, align: 'right', aggFunc: 'sum', valueFormatter: yen },
];

<SpreadsheetGrid rows={rows} columns={columns} totalRow />

Columns without aggFunc are blank. The label (default 「合計」, "Total" in English) goes in the first column, not counting the synthetic row drag and detail toggle columns. When the first column has a value, the label moves to the row number cell.

Options

Pass an object to totalRow to change the label, values, rendering and height.

totalRow={{
  label: 'Grand total',                              // label (default 'Total' in English)
  values: { customer: `${customerCount} customers` }, // show this value instead of computing (wins over aggFunc)
  height: 36,                                         // default is rowHeight
  className: 'my-total-row',                          // class added to the row element
}}
OptionDescription
labelThe label. Default '合計' ('Total' with the English texts).
valuesColumn key → value to show. Those columns show this value instead of computing. serverSide uses only this.
renderCellReplace the contents of cells that have a value (below).
heightHeight of the total row (px). Default rowHeight.
classNameClass added to the row element (or { className, style }).

Aggregation rules

  • Rows counted: data rows after filtering and sorting. Label rows aren't counted; with row grouping, rows in collapsed groups are counted too.
  • What counts as a number: the same as row grouping aggregates; only numbers and numeric strings are counted (empty values, whitespace-only strings, true / false, Date and so on are not). count is the number of rows counted. sum / avg / min / max with 0 numeric values are empty cells.
  • Custom aggregates: pass a function to aggFunc, and it receives { values, rows, column } and its return value is shown as is (the total row is computed with the same function as group rows).
  • When it computes: every time rows, columns, filters or sorting change, it recounts the rows in one pass (the same as row grouping aggregates; tens of ms for 1 million rows × 2 columns). It doesn't recount when only the contents of values change.
{
  key: 'status',
  title: 'Status',
  width: 100,
  aggFunc: ({ values }) => `On hold: ${values.filter((v) => v === 'On hold').length}`,
}

Display and formatting

  • Values are formatted with the column's valueFormatter. The total row has no source row, so the row passed to the formatter is undefined (the same as group rows). For columns whose formatter reads row, format with renderCell or values.
  • Strings (custom aggregates and strings in values) are shown as is. count and numbers in columns without valueFormatter use the same format as the selection aggregates in the bottom bar (thousands separators, up to 4 decimals, floating-point errors like 0.1 + 0.2 rounded).
  • The column's renderCell / cellClassName aren't used for the total row (there's no row data). To change the contents of total row cells, use totalRow.renderCell.
  • Auto-sizing columns (the column menu, double-clicking a border, auto-size all columns) makes room for the total row's text (kind + value) too. Values cut off by the column width show in full in a tooltip on hover.
totalRow={{
  renderCell: ({ column, value, kind, text, caption }) =>
    column.key === 'amount' ? <strong className="text-blue-700">{text}</strong> : (
      <>
        {caption && <small>{caption}</small>} {text}
      </>
    ),
}}

ctx.kind is 'sum' | 'avg' | 'min' | 'max' | 'count' | 'custom' | 'value' ('value' is a value passed with values), ctx.text is the default display text, and ctx.caption is the label for the kind (Avg / Min / Max / Count; undefined for sum, custom and values).

Scrolling and keyboard

  • With few rows, the total row comes right after the last row. With many rows, it stays at the bottom of the table.
  • Scrolled to the end, the last row sits right above the total row. Moving with the arrow keys and showing rows with scrollToRow / selectCell never hides them behind the total row.
  • Showing the total row while scrolled to the end keeps the last row visible.
  • Moving the pointer over the total row while dragging a range, a row, or the fill handle counts as the last visible row of the body. Auto-scrolling starts at the top edge of the total row.
  • The total row isn't counted in the row count (Rows in the bar) or in view row indexes.

Export

Not included by default. Pass includeTotalRow: true to add one row at the end, only with scope 'view' (the default). Values are raw, and the label goes in the first column only when the first column has no value.

gridRef.current?.exportCsv({ includeTotalRow: true });
const data = gridRef.current?.getExportData({ includeTotalRow: true });
data?.rowKinds; // ['data', 'data', …, 'total']

serverSide (dataSource)

The server-side row model doesn't hold every row, so the total row isn't computed. Pass values aggregated on the server with values (columns with only aggFunc are blank).

<SpreadsheetGrid dataSource={dataSource} columns={columns} totalRow={{ values: serverTotals }} />

Customizing the look

KindName
CSS variables--ssg-total-row-bg / --ssg-total-row-text / --ssg-total-row-border (the separator line on top)
Classes.ssg-total-row (row) / .ssg-total-cell (cell; also has .ssg-body-cell) / .ssg-total-caption (the kind label) / .ssg-total-label (the label text)
SlotsclassNames.totalRow (row element) / classNames.totalCell (cell)

Assistive technology reads it as the last row of the table (role="row"), and 1 row is added to aria-rowcount.

On this page