Data Grid - Server-side aggregation 🧪
Aggregation with server-side data source.
To dynamically load tree data from the server, you must create a data source and pass the unstable_dataSource
prop to the Data Grid, as detailed in the Server-side data overview.
Server-side aggregation requires some additional steps to implement:
Pass the available aggregation functions of type
GridAggregationFunctionDataSource
to the Data Grid using theaggregationFunctions
prop. Its default value is empty when the Data Grid is used with server-side data.const aggregationFunctions: Record<string, GridAggregationFunctionDataSource> = { size: { label: 'Size' }, sum: { label: 'Sum', columnTypes: ['number'] }, } <DataGridPremium aggregationFunctions={aggregationFunctions} />
The
GridAggregationFunctionDataSource
interface is similar toGridAggregationFunction
, but it doesn't haveapply
orgetCellValue
properties because the computation is done on the server.See the GridAggregationFunctionDataSource API page for more details.
Use
aggregationModel
passed in thegetRows
method ofGridDataSource
to fetch the aggregated values. For the root level footer aggregation row, passaggregateRow
containing the aggregated values in theGetRowsResponse
.const dataSource = { getRows: async ({ sortModel, filterModel, paginationModel, + aggregationModel, }) => { const rows = await fetchRows(); - const response = await fetchData({ sortModel, filterModel, paginationModel }); + const response = await fetchData({ sortModel, filterModel, paginationModel, aggregationModel }); return { rows: response.rows, rowCount: getRowsResponse.totalCount, + aggregateRow: response.aggregateRow, } } }
Pass the getter method
getAggregatedValue
inGridDataSource
that defines how to get the aggregated value for a parent row (including theaggregateRow
).const dataSource = { getRows: async ({ ... }) => { ... }, getAggregatedValue: (row, field) => { return row[`${field}Aggregate`]; }, }
The following example demonstrates basic server-side aggregation.
Usage with lazy loading
Server-side aggregation can be implemented along with server-side lazy loading as shown in the demo below.
Usage with row grouping
Server-side aggregation works with row grouping in a similar way as described in Aggregation—usage with row grouping.
The aggregated values are acquired from the parent rows using the getAggregatedValue
method.
Usage with tree data
Server-side aggregation can be used with tree data in a similar way as described in Aggregation—usage with tree data.
The aggregated values are acquired from the parent rows using the getAggregatedValue
method.