@@ -19,23 +19,20 @@ import {
19
19
import { getAggregationRules } from './gridAggregationUtils' ;
20
20
import { gridAggregationModelSelector } from './gridAggregationSelectors' ;
21
21
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 } ) ;
35
34
const filteredRowsLookup = gridFilteredRowsLookupSelector ( apiRef ) ;
36
- const rowIds : GridRowId [ ] = apiRef . current . getRowGroupChildren ( { groupId } ) ;
37
35
38
- const values : any [ ] = [ ] ;
39
36
rowIds . forEach ( ( rowId ) => {
40
37
if ( aggregationRowsScope === 'filtered' && filteredRowsLookup [ rowId ] === false ) {
41
38
return ;
@@ -53,51 +50,43 @@ const getAggregationCellValue = ({
53
50
return ;
54
51
}
55
52
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 ) ;
63
54
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 ] ;
70
58
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 ;
87
61
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
+ } ) ;
91
86
92
87
groupAggregationLookup [ aggregatedField ] = {
93
88
position,
94
- value : getAggregationCellValue ( {
95
- apiRef,
96
- groupId,
97
- field : aggregatedField ,
98
- aggregationFunction : columnAggregationRules . aggregationFunction ,
99
- aggregationRowsScope,
100
- } ) ,
89
+ value,
101
90
} ;
102
91
}
103
92
@@ -115,11 +104,11 @@ export const createAggregationLookup = ({
115
104
aggregationRowsScope : DataGridPremiumProcessedProps [ 'aggregationRowsScope' ] ;
116
105
getAggregationPosition : DataGridPremiumProcessedProps [ 'getAggregationPosition' ] ;
117
106
} ) : GridAggregationLookup => {
118
- const aggregationRules = getAggregationRules ( {
119
- columnsLookup : gridColumnLookupSelector ( apiRef ) ,
120
- aggregationModel : gridAggregationModelSelector ( apiRef ) ,
107
+ const aggregationRules = getAggregationRules (
108
+ gridColumnLookupSelector ( apiRef ) ,
109
+ gridAggregationModelSelector ( apiRef ) ,
121
110
aggregationFunctions ,
122
- } ) ;
111
+ ) ;
123
112
124
113
const aggregatedFields = Object . keys ( aggregationRules ) ;
125
114
if ( aggregatedFields . length === 0 ) {
@@ -143,14 +132,14 @@ export const createAggregationLookup = ({
143
132
if ( hasAggregableChildren ) {
144
133
const position = getAggregationPosition ( groupNode ) ;
145
134
if ( position != null ) {
146
- aggregationLookup [ groupNode . id ] = getGroupAggregatedValue ( {
147
- groupId : groupNode . id ,
135
+ aggregationLookup [ groupNode . id ] = getGroupAggregatedValue (
136
+ groupNode . id ,
148
137
apiRef ,
149
- aggregatedFields,
150
138
aggregationRowsScope ,
139
+ aggregatedFields ,
151
140
aggregationRules ,
152
141
position ,
153
- } ) ;
142
+ ) ;
154
143
}
155
144
}
156
145
} ;
0 commit comments