@@ -19,23 +19,20 @@ import {
1919import { getAggregationRules } from './gridAggregationUtils' ;
2020import { gridAggregationModelSelector } from './gridAggregationSelectors' ;
2121
22- const getAggregationCellValue = ( {
23- apiRef,
24- groupId,
25- field,
26- aggregationFunction,
27- aggregationRowsScope,
28- } : {
29- apiRef : React . MutableRefObject < GridApiPremium > ;
30- groupId : GridRowId ;
31- field : string ;
32- aggregationFunction : GridAggregationFunction ;
33- aggregationRowsScope : DataGridPremiumProcessedProps [ 'aggregationRowsScope' ] ;
34- } ) => {
22+ const getGroupAggregatedValue = (
23+ groupId : GridRowId ,
24+ apiRef : React . MutableRefObject < GridApiPremium > ,
25+ aggregationRowsScope : DataGridPremiumProcessedProps [ 'aggregationRowsScope' ] ,
26+ aggregatedFields : string [ ] ,
27+ aggregationRules : GridAggregationRules ,
28+ position : GridAggregationPosition ,
29+ ) => {
30+ const groupAggregationLookup : GridAggregationLookup [ GridRowId ] = { } ;
31+ const aggregatedValues : { aggregatedField : string ; values : any [ ] } [ ] = [ ] ;
32+
33+ const rowIds = apiRef . current . getRowGroupChildren ( { groupId } ) ;
3534 const filteredRowsLookup = gridFilteredRowsLookupSelector ( apiRef ) ;
36- const rowIds : GridRowId [ ] = apiRef . current . getRowGroupChildren ( { groupId } ) ;
3735
38- const values : any [ ] = [ ] ;
3936 rowIds . forEach ( ( rowId ) => {
4037 if ( aggregationRowsScope === 'filtered' && filteredRowsLookup [ rowId ] === false ) {
4138 return ;
@@ -53,51 +50,43 @@ const getAggregationCellValue = ({
5350 return ;
5451 }
5552
56- if ( typeof aggregationFunction . getCellValue === 'function' ) {
57- const row = apiRef . current . getRow ( rowId ) ;
58- values . push ( aggregationFunction . getCellValue ( { row } ) ) ;
59- } else {
60- values . push ( apiRef . current . getCellValue ( rowId , field ) ) ;
61- }
62- } ) ;
53+ const row = apiRef . current . getRow ( rowId ) ;
6354
64- return aggregationFunction . apply ( {
65- values,
66- groupId,
67- field, // Added per user request in https://github.com/mui/mui-x/issues/6995#issuecomment-1327423455
68- } ) ;
69- } ;
55+ for ( let j = 0 ; j < aggregatedFields . length ; j += 1 ) {
56+ const aggregatedField = aggregatedFields [ j ] ;
57+ const columnAggregationRules = aggregationRules [ aggregatedField ] ;
7058
71- const getGroupAggregatedValue = ( {
72- groupId,
73- apiRef,
74- aggregationRowsScope,
75- aggregatedFields,
76- aggregationRules,
77- position,
78- } : {
79- groupId : GridRowId ;
80- apiRef : React . MutableRefObject < GridApiPremium > ;
81- aggregationRowsScope : DataGridPremiumProcessedProps [ 'aggregationRowsScope' ] ;
82- aggregatedFields : string [ ] ;
83- aggregationRules : GridAggregationRules ;
84- position : GridAggregationPosition ;
85- } ) => {
86- const groupAggregationLookup : GridAggregationLookup [ GridRowId ] = { } ;
59+ const aggregationFunction = columnAggregationRules . aggregationFunction ;
60+ const field = aggregatedField ;
8761
88- for ( let j = 0 ; j < aggregatedFields . length ; j += 1 ) {
89- const aggregatedField = aggregatedFields [ j ] ;
90- const columnAggregationRules = aggregationRules [ aggregatedField ] ;
62+ if ( aggregatedValues [ j ] === undefined ) {
63+ aggregatedValues [ j ] = {
64+ aggregatedField,
65+ values : [ ] ,
66+ } ;
67+ }
68+
69+ if ( typeof aggregationFunction . getCellValue === 'function' ) {
70+ aggregatedValues [ j ] . values . push ( aggregationFunction . getCellValue ( { row } ) ) ;
71+ } else {
72+ const colDef = apiRef . current . getColumn ( field ) ;
73+ aggregatedValues [ j ] . values . push ( apiRef . current . getRowValue ( row , colDef ) ) ;
74+ }
75+ }
76+ } ) ;
77+
78+ for ( let i = 0 ; i < aggregatedValues . length ; i += 1 ) {
79+ const { aggregatedField, values } = aggregatedValues [ i ] ;
80+ const aggregationFunction = aggregationRules [ aggregatedField ] . aggregationFunction ;
81+ const value = aggregationFunction . apply ( {
82+ values,
83+ groupId,
84+ field : aggregatedField , // Added per user request in https://github.com/mui/mui-x/issues/6995#issuecomment-1327423455
85+ } ) ;
9186
9287 groupAggregationLookup [ aggregatedField ] = {
9388 position,
94- value : getAggregationCellValue ( {
95- apiRef,
96- groupId,
97- field : aggregatedField ,
98- aggregationFunction : columnAggregationRules . aggregationFunction ,
99- aggregationRowsScope,
100- } ) ,
89+ value,
10190 } ;
10291 }
10392
@@ -115,11 +104,11 @@ export const createAggregationLookup = ({
115104 aggregationRowsScope : DataGridPremiumProcessedProps [ 'aggregationRowsScope' ] ;
116105 getAggregationPosition : DataGridPremiumProcessedProps [ 'getAggregationPosition' ] ;
117106} ) : GridAggregationLookup => {
118- const aggregationRules = getAggregationRules ( {
119- columnsLookup : gridColumnLookupSelector ( apiRef ) ,
120- aggregationModel : gridAggregationModelSelector ( apiRef ) ,
107+ const aggregationRules = getAggregationRules (
108+ gridColumnLookupSelector ( apiRef ) ,
109+ gridAggregationModelSelector ( apiRef ) ,
121110 aggregationFunctions ,
122- } ) ;
111+ ) ;
123112
124113 const aggregatedFields = Object . keys ( aggregationRules ) ;
125114 if ( aggregatedFields . length === 0 ) {
@@ -143,14 +132,14 @@ export const createAggregationLookup = ({
143132 if ( hasAggregableChildren ) {
144133 const position = getAggregationPosition ( groupNode ) ;
145134 if ( position != null ) {
146- aggregationLookup [ groupNode . id ] = getGroupAggregatedValue ( {
147- groupId : groupNode . id ,
135+ aggregationLookup [ groupNode . id ] = getGroupAggregatedValue (
136+ groupNode . id ,
148137 apiRef ,
149- aggregatedFields,
150138 aggregationRowsScope ,
139+ aggregatedFields ,
151140 aggregationRules ,
152141 position ,
153- } ) ;
142+ ) ;
154143 }
155144 }
156145 } ;
0 commit comments