UNPKG

@mui/x-data-grid-premium

Version:

The Premium plan edition of the MUI X Data Grid Components.

111 lines (110 loc) 4.32 kB
import { gridColumnLookupSelector, gridFilteredRowsLookupSelector, gridRowTreeSelector, GRID_ROOT_GROUP_ID, gridRowNodeSelector } from '@mui/x-data-grid-pro'; import { getAggregationRules } from "./gridAggregationUtils.js"; import { gridAggregationModelSelector } from "./gridAggregationSelectors.js"; const getGroupAggregatedValue = (groupId, apiRef, aggregationRowsScope, aggregatedFields, aggregationRules, position) => { const groupAggregationLookup = {}; const aggregatedValues = []; const rowIds = apiRef.current.getRowGroupChildren({ groupId }); const filteredRowsLookup = gridFilteredRowsLookupSelector(apiRef); rowIds.forEach(rowId => { if (aggregationRowsScope === 'filtered' && filteredRowsLookup[rowId] === false) { return; } // If the row is a group, we want to aggregate based on its children // For instance in the following tree, we want the aggregated values of A to be based on A.A, A.B.A and A.B.B but not A.B // A // A.A // A.B // A.B.A // A.B.B const rowNode = gridRowNodeSelector(apiRef, rowId); if (rowNode.type === 'group') { return; } const row = apiRef.current.getRow(rowId); for (let j = 0; j < aggregatedFields.length; j += 1) { const aggregatedField = aggregatedFields[j]; const columnAggregationRules = aggregationRules[aggregatedField]; const aggregationFunction = columnAggregationRules.aggregationFunction; const field = aggregatedField; if (aggregatedValues[j] === undefined) { aggregatedValues[j] = { aggregatedField, values: [] }; } if (typeof aggregationFunction.getCellValue === 'function') { aggregatedValues[j].values.push(aggregationFunction.getCellValue({ row })); } else { const colDef = apiRef.current.getColumn(field); aggregatedValues[j].values.push(apiRef.current.getRowValue(row, colDef)); } } }); for (let i = 0; i < aggregatedValues.length; i += 1) { const { aggregatedField, values } = aggregatedValues[i]; const aggregationFunction = aggregationRules[aggregatedField].aggregationFunction; const value = aggregationFunction.apply({ values, groupId, field: aggregatedField // Added per user request in https://github.com/mui/mui-x/issues/6995#issuecomment-1327423455 }); groupAggregationLookup[aggregatedField] = { position, value }; } return groupAggregationLookup; }; const getGroupAggregatedValueDataSource = (groupId, apiRef, aggregatedFields, position) => { const groupAggregationLookup = {}; for (let j = 0; j < aggregatedFields.length; j += 1) { const aggregatedField = aggregatedFields[j]; groupAggregationLookup[aggregatedField] = { position, value: apiRef.current.resolveGroupAggregation?.(groupId, aggregatedField) ?? '' }; } return groupAggregationLookup; }; export const createAggregationLookup = ({ apiRef, aggregationFunctions, aggregationRowsScope, getAggregationPosition, isDataSource }) => { const aggregationRules = getAggregationRules(gridColumnLookupSelector(apiRef), gridAggregationModelSelector(apiRef), aggregationFunctions, isDataSource); const aggregatedFields = Object.keys(aggregationRules); if (aggregatedFields.length === 0) { return {}; } const aggregationLookup = {}; const rowTree = gridRowTreeSelector(apiRef); const createGroupAggregationLookup = groupNode => { for (let i = 0; i < groupNode.children.length; i += 1) { const childId = groupNode.children[i]; const childNode = rowTree[childId]; if (childNode.type === 'group') { createGroupAggregationLookup(childNode); } } const position = getAggregationPosition(groupNode); if (position !== null) { if (isDataSource) { aggregationLookup[groupNode.id] = getGroupAggregatedValueDataSource(groupNode.id, apiRef, aggregatedFields, position); } else if (groupNode.children.length) { aggregationLookup[groupNode.id] = getGroupAggregatedValue(groupNode.id, apiRef, aggregationRowsScope, aggregatedFields, aggregationRules, position); } } }; createGroupAggregationLookup(rowTree[GRID_ROOT_GROUP_ID]); return aggregationLookup; };