@@ -21,8 +21,6 @@ export class IgxPivotRowPipe implements PipeTransform {
2121 rows : IPivotDimension [ ] ,
2222 values ?: IPivotValue [ ]
2323 ) : any [ ] {
24-
25-
2624 // build hierarchies - groups and subgroups
2725 const hierarchies = PivotUtil . getFieldsHierarchy ( collection , rows ) ;
2826 // apply aggregations based on the created groups
@@ -47,13 +45,32 @@ export class IgxPivotColumnPipe implements PipeTransform {
4745 columns : IPivotDimension [ ] ,
4846 values ?: IPivotValue [ ]
4947 ) : any [ ] {
50- // build hierarchies - groups and subgroups
51- const hierarchies = PivotUtil . getFieldsHierarchy ( collection , columns ) ;
52- // apply aggregations based on the created groups
53- PivotUtil . applyAggregations ( hierarchies , values ) ;
54- // generate column fields based on the hierarchies
48+ // build hierarchies - groups and subgroups by columns
49+ const result = [ ] ;
50+ collection . forEach ( hierarchy => {
51+ // apply aggregations based on the created groups and generate column fields based on the hierarchies
52+ this . groupColumns ( hierarchy , columns , values ) ;
53+ if ( hierarchy [ 'children' ] ) {
54+ let flatCols = { } ;
55+ PivotUtil . flattenColumnHierarchy ( hierarchy [ 'children' ] , values ) . forEach ( o => {
56+ delete o [ 'records' ] ;
57+ flatCols = { ...flatCols , ...o } ;
58+ } ) ;
59+ result . push ( { ...hierarchy , ...flatCols } ) ;
60+ }
61+ } ) ;
5562
56- return [ ] ;
63+ return result ;
64+ }
65+
66+ private groupColumns ( hierarchy , columns , values ) {
67+ const children = hierarchy [ 'children' ] ;
68+ if ( children ) {
69+ this . groupColumns ( children , columns , values ) ;
70+ } else if ( hierarchy [ 'records' ] ) {
71+ hierarchy [ 'children' ] = PivotUtil . getFieldsHierarchy ( hierarchy [ 'records' ] , columns ) ;
72+ PivotUtil . applyAggregations ( hierarchy [ 'children' ] , values ) ;
73+ }
5774 }
5875
5976
@@ -165,6 +182,23 @@ export class PivotUtil {
165182 return flatData ;
166183 }
167184
185+ public static flattenColumnHierarchy ( hierarchies , values ) {
186+ let flatData = [ ] ;
187+ hierarchies . forEach ( ( h , key ) => {
188+ const obj = { } ;
189+ for ( const value of values ) {
190+ obj [ key ] = h [ 'aggregations' ] [ value . member ] ;
191+ obj [ 'records' ] = h [ 'records' ] ;
192+ flatData . push ( obj ) ;
193+ if ( h [ 'children' ] ) {
194+ flatData = [ ...flatData , ...this . flattenColumnHierarchy ( h [ 'children' ] , values ) ] ;
195+ }
196+ }
197+ } ) ;
198+
199+ return flatData ;
200+ }
201+
168202 private static generateFieldValue ( rec ) {
169203 let i = 0 ;
170204 while ( Object . keys ( rec ) . indexOf ( 'field' + ++ i ) !== - 1 ) { }
0 commit comments