1. Data Grid docs
  2. Working with records

Totals and the totals row

A totals row pinned under the records, with a sum, average, minimum, maximum or count for each column over every matching record.

The totals row stays pinned under the records and shows one aggregate per column: a sum, an average, a minimum, a maximum or a count. It covers every record that matches the current search and filters, not only the rows on screen, and it updates as they change.

Open the columns example, with a totals row

In the grid

  • Totals in the toolbar shows or hides the row. Turning it on sums every number column that has no aggregate yet.
  • Each cell of the row is a menu: None, Sum, Average, Minimum, Maximum or Count. Count works for any column; the others need a number or date column.
  • For a total per group, use the toolbar's Summary (see Sorting, grouping and views).

From code

setTotalsRow(on) shows or hides the row; turning it on sums each number column that has no aggregate yet. setColumn(field, { total }) picks a column's aggregate ('sum', 'average', 'min', 'max', 'count' or null). getTotals() reads the values as display text: { summary, totals }, where summary is the toolbar's Summary over every matching record. In the state, these are totalsRow and each column's total.

JavaScript

import { mountTable } from '@8sheets/data-grid';

const grid = await mountTable(document.querySelector('#orders'), { title: 'Orders', data: orders });

await grid.setTotalsRow(true);
await grid.setColumn('Units', { total: 'average' });
await grid.setColumn('Price', { total: 'max' });
await grid.setColumn('Order', { total: 'count' });

// Show the revenue total in your own header too, and keep it current.
grid.on('stateChange', () => (header.textContent = grid.getTotals().totals.Revenue ?? ''));

React

import { DataGrid } from '@8sheets/data-grid-react';

async function withTotals(grid) {
  await grid.setTotalsRow(true);
  await grid.setColumn('Units', { total: 'average' });
  await grid.setColumn('Price', { total: 'max' });
}

export function Orders({ options }) {
  return <div style={{ height: 480 }}><DataGrid options={options} onReady={withTotals} /></div>;
}

Vue

<script setup>
import { DataGrid } from '@8sheets/data-grid-vue';

defineProps({ options: Object });
async function withTotals(grid) {
  await grid.setTotalsRow(true);
  await grid.setColumn('Units', { total: 'average' });
  await grid.setColumn('Price', { total: 'max' });
}
</script>

<template>
  <div style="height: 480px"><DataGrid :options="options" @ready="withTotals" /></div>
</template>

Angular

import { Component, Input } from '@angular/core';
import { DataGridComponent, DataGridOptions, MountedDataGrid } from '@8sheets/data-grid-angular';

@Component({
  selector: 'app-orders',
  imports: [DataGridComponent],
  template: `<eight-data-grid [options]="options" (ready)="withTotals($event)" style="height: 480px" />`,
})
export class Orders {
  @Input() options: DataGridOptions = {};

  async withTotals(grid: MountedDataGrid) {
    await grid.setTotalsRow(true);
    await grid.setColumn('Units', { total: 'average' });
    await grid.setColumn('Price', { total: 'max' });
  }
}

With a server source

The server receives totalsRow and each column's total in the view state, and returns the display text of each aggregate in TablePage.totals, keyed by field, computed over the whole result:

{ "offset": 0, "rows": [], "columns": ["Units", "Revenue"], "summary": "", "totals": { "Units": "12.4", "Revenue": "4,182,550.00" } }

tableQueryService computes them for you.

Limits

  • A flat export holds the records, not the totals row. An XLSX or PDF export with layout: true keeps it (see Export).
  • Sum, average, minimum and maximum skip text and blank cells; count counts non-blank cells.
  • An average of whole numbers is shown with up to two decimals; the other aggregates use the column's number format.