@@ -61,6 +61,10 @@ export class IgxPivotColumnPipe implements PipeTransform {
6161 } ) ;
6262 delete hierarchy [ pivotKeys . children ] ; /* or we can keep it
6363 and use when creating the columns in pivot grid instead of recreating it */
64+ if ( this . isLeaf ( hierarchy , pivotKeys ) ) {
65+ delete hierarchy [ pivotKeys . records ] ; /* remove the helper records of the actual records so that
66+ expand indicators can be rendered properly */
67+ }
6468 result . push ( { ...hierarchy , ...flatCols } ) ;
6569 }
6670 } ) ;
@@ -73,11 +77,28 @@ export class IgxPivotColumnPipe implements PipeTransform {
7377 if ( children ) {
7478 this . groupColumns ( children , columns , values , pivotKeys ) ;
7579 } else if ( hierarchy [ pivotKeys . records ] ) {
76- hierarchy [ pivotKeys . children ] = PivotUtil . getFieldsHierarchy ( hierarchy [ pivotKeys . records ] , columns , pivotKeys ) ;
80+ const leafRecords = this . getLeafs ( hierarchy [ pivotKeys . records ] , pivotKeys ) ;
81+ hierarchy [ pivotKeys . children ] = PivotUtil . getFieldsHierarchy ( leafRecords , columns , pivotKeys ) ;
7782 PivotUtil . applyAggregations ( hierarchy [ pivotKeys . children ] , values , pivotKeys ) ;
7883 }
7984 }
8085
86+ private getLeafs ( records , pivotKeys ) {
87+ let leafs = [ ] ;
88+ for ( const rec of records ) {
89+ if ( rec [ pivotKeys . records ] ) {
90+ leafs = leafs . concat ( this . getLeafs ( rec [ pivotKeys . records ] , pivotKeys ) ) ;
91+ } else {
92+ leafs . push ( rec ) ;
93+ }
94+ }
95+ return leafs ;
96+ }
97+
98+ private isLeaf ( record , pivotKeys ) {
99+ return record [ pivotKeys . records ] && record [ pivotKeys . records ] . some ( r => r [ pivotKeys . records ] ) ;
100+ }
101+
81102
82103}
83104
@@ -170,20 +191,19 @@ export class PivotUtil {
170191 return result ;
171192 }
172193
173- public static flattenHierarchy ( hierarchies , rec , pivotKeys ) {
194+ public static flattenHierarchy ( hierarchies , rec , pivotKeys , level = 0 ) {
174195 let flatData = [ ] ;
175196 const field = this . generateFieldValue ( rec ) ;
176197 hierarchies . forEach ( ( h , key ) => {
177198 let obj = { } ;
178199 obj [ field ] = key ;
179200 obj [ pivotKeys . records ] = h [ pivotKeys . records ] ;
180201 obj = { ...obj , ...h [ pivotKeys . aggregations ] } ;
202+ obj [ pivotKeys . level ] = level ;
181203 flatData . push ( obj ) ;
182204 if ( h [ pivotKeys . children ] ) {
183- let childRecords = [ ] ;
184- h [ pivotKeys . children ] . forEach ( c => childRecords = [ ...childRecords , ...c [ pivotKeys . records ] ] ) ;
185- obj [ pivotKeys . records ] = obj [ pivotKeys . records ] ? [ ...obj [ pivotKeys . records ] , ...childRecords ] : childRecords ;
186- flatData = [ ...flatData , ...this . flattenHierarchy ( h [ pivotKeys . children ] , rec , pivotKeys ) ] ;
205+ obj [ pivotKeys . records ] = this . flattenHierarchy ( h [ pivotKeys . children ] , rec , pivotKeys , level + 1 ) ;
206+ flatData = [ ...flatData , ...obj [ pivotKeys . records ] ] ;
187207 }
188208 } ) ;
189209
0 commit comments