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)
- 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
}}| Option | Description |
|---|---|
label | The label. Default '合計' ('Total' with the English texts). |
values | Column key → value to show. Those columns show this value instead of computing. serverSide uses only this. |
renderCell | Replace the contents of cells that have a value (below). |
height | Height of the total row (px). Default rowHeight. |
className | Class 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,Dateand so on are not).countis 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
valueschange.
{
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 therowpassed to the formatter isundefined(the same as group rows). For columns whose formatter readsrow, format withrenderCellorvalues. - Strings (custom aggregates and strings in
values) are shown as is.countand numbers in columns withoutvalueFormatteruse the same format as the selection aggregates in the bottom bar (thousands separators, up to 4 decimals, floating-point errors like0.1 + 0.2rounded). - The column's
renderCell/cellClassNamearen't used for the total row (there's no row data). To change the contents of total row cells, usetotalRow.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/selectCellnever 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
| Kind | Name |
|---|---|
| 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) |
| Slots | classNames.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.
Tree data
Turn flat rows into a parent-child hierarchy with treeData, with open / close, parent aggregates, and filtering and sorting within siblings
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)