View as Markdown

Aggregates

Aggregates

Footer, group footer and group caption summaries with built-in and custom aggregate types.

Aggregates are a core module declared as data: one or more aggregate rows, each listing fields with one or several types, as the aggregates prop or as <DataGridAggregateRow> and <DataGridAggregate> children. The built-in types are sum, average, min, max, count, true count and false count; a custom type receives the rows of the grid or group. Values use number and date formats.

<DataGrid
  data={orders}
  columns={columns}
  grouping={{ groupBy: ['country'] }}
  aggregates={[
    {
      columns: [
        { field: 'amount', type: ['sum', 'average'], format: 'C2',
          renderFooter: ({ formatted }) => <>Total {formatted.sum}</>,
          renderGroupCaption: ({ formatted }) => <>Avg {formatted.average}</> },
        { field: 'shipped', type: 'trueCount', renderGroupFooter: ({ values }) => <>{String(values.trueCount)} shipped</> },
        { field: 'customer', type: 'custom', custom: (rows) => new Set(rows.map((row) => row.customer)).size },
      ],
    },
  ]}
/>

Each DataGridAggregate names the field, the type (one or an array: 'sum', 'average', 'min', 'max', 'count', 'trueCount', 'falseCount', 'custom'), an optional displayField for the column the value shows under, a format and the templates. The JSX form is <DataGridAggregateRow><DataGridAggregate field="amount" type="sum" /></DataGridAggregateRow> inside the grid. api.getAggregates() returns the footer values and api.getAggregates(groupId) those of one group.

Placement and templates

Each aggregate shows in the grid footer, in group footers, in group captions, or several of these: the templates it defines decide where, and without any template it shows in the footer with the default rendering. Footer, group footer and group caption templates are functions that receive the raw and formatted values, the group, its count and its rows.

String templates such as ${Sum} are not supported (as for column templates): templates are functions, so no string evaluation runs on the footer render path and they are type-checked. For a footer that reads "Sum: ...", use renderFooter: ({ formatted }) => <>Sum: {formatted.sum}</>; the context carries the values (values) and formatted text (formatted) by type.

The footer stays pinned to the bottom of the scroll container.

Correctness

Aggregates are computed over the whole filtered data set, not over the rows that happen to be rendered, so they are correct under virtualization. They recompute when the query or the data changes.

With a remote data source that aggregates on the server, the result lists 'aggregate' in performed and carries the totals in aggregates: the footer and api.getAggregates() then show those server totals instead of summarizing the received page. Group footers and captions still summarize the received rows, except in lazy grouping, where captions come with server aggregates.

Aggregates

Open demo

Footer, group footer and group caption aggregates over 20,000 virtualized rows; several types per column, two aggregate rows, a custom aggregate, formats, function templates, and recomputation on filter.

Order
Country
Category
Qty
Amount
Shipped
Country: France - 4,425 itemsΣ $1,110,207.84
ORD-10007
France
Tea
10
$108.38
ORD-10016
France
Equipment
4
$464.40
ORD-10017
France
Equipment
6
$214.20
ORD-10023
France
Coffee
6
$84.00
ORD-10032
France
Tea
6
$72.68
ORD-10034
France
Tea
9
$114.75
ORD-10041
France
Coffee
4
$62.90
ORD-10052
France
Equipment
11
$1,348.05
ORD-10053
France
Coffee
4
$56.00
ORD-10079
France
Coffee
6
$79.80
ORD-10080
France
Tea
12
$137.70
ORD-10081
France
Tea
10
$83.25
ORD-10082
France
Equipment
12
$504.00
ORD-10089
France
Equipment
6
$774.00
ORD-10092
France
Equipment
8
$980.40
ORD-10093
France
Equipment
4
$438.60
ORD-10094
France
Equipment
5
$199.50
ORD-10096
France
Coffee
10
$133.00
ORD-10097
France
Equipment
8
$928.80

Aggregates with paging

With paging, the grid prop aggregateScope picks what the footer and the group footers and captions summarize: 'page' (the default) aggregates the rows of the current page, 'all' every row that matches the filters. With grouping, pages count data rows and a group that spans two pages shows its caption on both; with 'page' its aggregates cover only the rows on the current page.

Paged groups and aggregate scope

Open demo

400 orders grouped by country and paged 25 data rows at a time; aggregateScope switches the footer and group footers between the current page and every row.

Aggregate scope
Order
Country
Customer
Product
Amount
Country: France - 25 items
ORD-10002
France
Bao Nguyen
Grinder
$857.85
ORD-10006
France
Bao Nguyen
Grinder
$877.20
ORD-10014
France
Aiko Tan
Espresso beans
$111.00
ORD-10022
France
Bao Nguyen
Espresso beans
$210.90
ORD-10023
France
Hana Sato
Grinder
$490.20
ORD-10025
France
Aiko Tan
Pour-over kit
$504.00
ORD-10030
France
Goran Petrov
Green tea
$58.28
ORD-10032
France
Jun Park
Green tea
$37.00
ORD-10046
France
Bao Nguyen
Grinder
$438.60
ORD-10050
France
Ines Duarte
Oolong tea
$75.86
ORD-10052
France
Farah Aziz
Kettle
$246.62
ORD-10059
France
Ines Duarte
Filter beans
$50.40
ORD-10066
France
Dewi Lestari
Kettle
$308.27
ORD-10074
France
Aiko Tan
Filter beans
$11.90
ORD-10080
France
Chen Wei
Grinder
$129.00
ORD-10086
France
Hana Sato
Kettle
$175.23
ORD-10093
France
Jun Park
Green tea
$55.04
ORD-10099
France
Hana Sato
Filter beans
$107.10
ORD-10103
France
Farah Aziz
Green tea
$47.18

Pages count data rows; captions and footers of the groups on a page come along. Change the page and watch the footer.