@@ -30,50 +30,50 @@ export class PivotRowDimensionsStrategy implements IPivotDimensionStrategy {
3030 }
3131
3232 public process (
33- collection : any ,
34- rows : IPivotDimension [ ] ,
35- values ?: IPivotValue [ ] ,
36- pivotKeys : IPivotKeys =
37- { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
38- ) : any [ ] {
39- let hierarchies ;
40- let data ;
41- const prevRowDims = [ ] ;
42- let prevDim ;
43- for ( const row of rows ) {
44- if ( ! data ) {
45- // build hierarchies - groups and subgroups
46- hierarchies = PivotUtil . getFieldsHierarchy ( collection , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
47- // generate flat data from the hierarchies
48- data = PivotUtil . processHierarchy ( hierarchies , collection [ 0 ] ?? [ ] , pivotKeys , 0 , true ) ;
49- prevRowDims . push ( row ) ;
50- prevDim = row ;
51- } else {
52- const newData = [ ...data ] ;
53- for ( let i = 0 ; i < newData . length ; i ++ ) {
54- const currData = newData [ i ] [ prevDim . memberName + '_' + pivotKeys . records ] ;
55- const hierarchyFields = PivotUtil
56- . getFieldsHierarchy ( currData , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
57- const siblingData = PivotUtil
58- . processHierarchy ( hierarchyFields , newData [ i ] ?? [ ] , pivotKeys , 0 ) ;
59- PivotUtil . processSiblingProperties ( newData [ i ] , siblingData , pivotKeys ) ;
60-
61- PivotUtil . processSubGroups ( row , prevRowDims . slice ( 0 ) , siblingData , pivotKeys ) ;
62- if ( PivotUtil . getDimensionDepth ( prevDim ) > PivotUtil . getDimensionDepth ( row ) ) {
63- newData [ i ] [ row . memberName + '_' + pivotKeys . records ] = siblingData ;
64- } else {
65- newData . splice ( i , 1 , ...siblingData ) ;
66- }
67- i += siblingData . length - 1 ;
33+ collection : any ,
34+ rows : IPivotDimension [ ] ,
35+ values ?: IPivotValue [ ] ,
36+ pivotKeys : IPivotKeys =
37+ { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
38+ ) : any [ ] {
39+ let hierarchies ;
40+ let data ;
41+ const prevRowDims = [ ] ;
42+ let prevDim ;
43+ for ( const row of rows ) {
44+ if ( ! data ) {
45+ // build hierarchies - groups and subgroups
46+ hierarchies = PivotUtil . getFieldsHierarchy ( collection , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
47+ // generate flat data from the hierarchies
48+ data = PivotUtil . processHierarchy ( hierarchies , collection [ 0 ] ?? [ ] , pivotKeys , 0 , true ) ;
49+ prevRowDims . push ( row ) ;
50+ prevDim = row ;
51+ } else {
52+ const newData = [ ...data ] ;
53+ for ( let i = 0 ; i < newData . length ; i ++ ) {
54+ const currData = newData [ i ] [ prevDim . memberName + '_' + pivotKeys . records ] ;
55+ const hierarchyFields = PivotUtil
56+ . getFieldsHierarchy ( currData , [ row ] , PivotDimensionType . Row , pivotKeys ) ;
57+ const siblingData = PivotUtil
58+ . processHierarchy ( hierarchyFields , newData [ i ] ?? [ ] , pivotKeys , 0 ) ;
59+ PivotUtil . processSiblingProperties ( newData [ i ] , siblingData , pivotKeys ) ;
60+
61+ PivotUtil . processSubGroups ( row , prevRowDims . slice ( 0 ) , siblingData , pivotKeys ) ;
62+ if ( PivotUtil . getDimensionDepth ( prevDim ) > PivotUtil . getDimensionDepth ( row ) ) {
63+ newData [ i ] [ row . memberName + '_' + pivotKeys . records ] = siblingData ;
64+ } else {
65+ newData . splice ( i , 1 , ...siblingData ) ;
6866 }
69- data = newData ;
70- prevDim = row ;
71- prevRowDims . push ( row ) ;
67+ i += siblingData . length - 1 ;
7268 }
69+ data = newData ;
70+ prevDim = row ;
71+ prevRowDims . push ( row ) ;
7372 }
74- return data ;
7573 }
74+ return data ;
7675 }
76+ }
7777
7878export class PivotColumnDimensionsStrategy implements IPivotDimensionStrategy {
7979 private static _instance : PivotRowDimensionsStrategy = null ;
@@ -83,43 +83,54 @@ export class PivotColumnDimensionsStrategy implements IPivotDimensionStrategy {
8383 }
8484
8585 public process (
86- collection : any [ ] ,
87- columns : IPivotDimension [ ] ,
88- values : IPivotValue [ ] ,
89- pivotKeys : IPivotKeys = { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
90- ) : any [ ] {
91- const result = [ ] ;
92- collection . forEach ( hierarchy => {
93- // apply aggregations based on the created groups and generate column fields based on the hierarchies
94- this . groupColumns ( hierarchy , columns , values , pivotKeys ) ;
95- if ( hierarchy [ pivotKeys . children ] ) {
96- let flatCols = { } ;
97- PivotUtil . flattenColumnHierarchy ( hierarchy [ pivotKeys . children ] , values , pivotKeys ) . forEach ( o => {
98- delete o [ pivotKeys . records ] ;
99- flatCols = { ...flatCols , ...o } ;
100- } ) ;
101- delete hierarchy [ pivotKeys . children ] ; /* or we can keep it
102- and use when creating the columns in pivot grid instead of recreating it */
103- const keys = Object . keys ( hierarchy ) ;
104- //remove all record keys from final data since we don't need them anymore.
105- keys . forEach ( k => {
106- if ( k . indexOf ( pivotKeys . records ) !== - 1 || k === pivotKeys . level ) {
107- delete hierarchy [ k ] ;
86+ collection : any [ ] ,
87+ columns : IPivotDimension [ ] ,
88+ values : IPivotValue [ ] ,
89+ pivotKeys : IPivotKeys = { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
90+ ) : any [ ] {
91+ const res = this . processHierarchy ( collection , columns , values , pivotKeys ) ;
92+ return res ;
93+ }
94+
95+ private processHierarchy ( collection , columns : IPivotDimension [ ] , values , pivotKeys ) {
96+ const result = [ ] ;
97+ collection . forEach ( hierarchy => {
98+ // apply aggregations based on the created groups and generate column fields based on the hierarchies
99+ this . groupColumns ( hierarchy , columns , values , pivotKeys ) ;
100+ if ( hierarchy [ pivotKeys . children ] ) {
101+ let flatCols = { } ;
102+ PivotUtil . flattenColumnHierarchy ( hierarchy [ pivotKeys . children ] , values , pivotKeys ) . forEach ( o => {
103+ delete o [ pivotKeys . records ] ;
104+ flatCols = { ...flatCols , ...o } ;
105+ } ) ;
106+ delete hierarchy [ pivotKeys . children ] ; /* or we can keep it
107+ and use when creating the columns in pivot grid instead of recreating it */
108+ const keys = Object . keys ( hierarchy ) ;
109+ //remove all record keys from final data since we don't need them anymore.
110+ keys . forEach ( k => {
111+ if ( k . indexOf ( pivotKeys . records ) !== - 1 ) {
112+ if ( hierarchy [ k ] ) {
113+ this . processHierarchy ( hierarchy [ k ] , columns , values , pivotKeys ) ;
108114 }
109- } ) ;
110- if ( this . isLeaf ( hierarchy , pivotKeys ) ) {
111- delete hierarchy [ pivotKeys . records ] ; /* remove the helper records of the actual records so that
112- expand indicators can be rendered properly */
115+ //delete hierarchy[k];
113116 }
114- for ( const property in flatCols ) {
115- if ( flatCols . hasOwnProperty ( property ) ) {
116- hierarchy [ property ] = flatCols [ property ] ;
117- }
117+ if ( k === pivotKeys . level ) {
118+ delete hierarchy [ k ] ;
118119 }
119- result . push ( hierarchy ) ;
120+ } ) ;
121+ if ( this . isLeaf ( hierarchy , pivotKeys ) ) {
122+ delete hierarchy [ pivotKeys . records ] ; /* remove the helper records of the actual records so that
123+ expand indicators can be rendered properly */
120124 }
121- } ) ;
122- return result ;
125+ for ( const property in flatCols ) {
126+ if ( flatCols . hasOwnProperty ( property ) ) {
127+ hierarchy [ property ] = flatCols [ property ] ;
128+ }
129+ }
130+ result . push ( hierarchy ) ;
131+ }
132+ } ) ;
133+ return result ;
123134 }
124135
125136 private groupColumns ( hierarchy , columns , values , pivotKeys ) {
0 commit comments