11import { Pipe , PipeTransform } from '@angular/core' ;
2- import { IFilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree' ;
2+ import { cloneArray } from '../../core/utils' ;
3+ import { DataUtil } from '../../data-operations/data-util' ;
4+ import { FilteringExpressionsTree , IFilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree' ;
35import { IFilteringStrategy } from '../../data-operations/filtering-strategy' ;
6+ import { IGroupingExpression } from '../../data-operations/grouping-expression.interface' ;
7+ import { SortingDirection } from '../../data-operations/sorting-expression.interface' ;
8+ import { DefaultSortingStrategy } from '../../data-operations/sorting-strategy' ;
49import { GridBaseAPIService } from '../api.service' ;
510import { IgxPivotGridComponent } from './pivot-grid.component' ;
611import { IPivotDimension , IPivotValue } from './pivot-grid.interface' ;
@@ -20,15 +25,61 @@ export class IgxPivotRowPipe implements PipeTransform {
2025 collection : any ,
2126 rows : IPivotDimension [ ] ,
2227 values ?: IPivotValue [ ]
23- ) : any [ ] {
24- return collection ;
28+ ) : any [ ] {
29+
30+ const result : any [ ] = collection . slice ( ) ;
31+ let groupingExpressions : IGroupingExpression [ ] = [ ] ;
32+
33+ // group the data in a way using the rows.member declarations in a groupingComparer
34+ for ( const row of rows ) {
35+ groupingExpressions = groupingExpressions . concat ( this . buildGroupingExpressions ( row ) ) ;
36+ }
37+
38+ // need to extend the grouping and improve the groupingComparer function capabilities
39+ const sorted = DataUtil . sort ( result , groupingExpressions ) ;
40+ const groupResult = DataUtil
41+ . group ( sorted , { defaultExpanded : true , expansion : [ ] , expressions : groupingExpressions } ) ;
42+
43+ // go around the data and aggregate by the specified values, aggregations should be
44+ // stored into the groups
45+ for ( const val of values ) {
46+ this . applyAggregation ( groupResult . data , val ) ;
47+ }
48+
49+ return groupResult . data ;
50+ }
51+
52+ private buildGroupingExpressions ( row : IPivotDimension ) : IGroupingExpression [ ] {
53+ let groupingExpressions : IGroupingExpression [ ] = [ {
54+ fieldName : row . name ,
55+ dir : SortingDirection . Asc ,
56+ groupingComparer : ( a , b ) => DefaultSortingStrategy . instance ( )
57+ . compareValues ( row . member . call ( this , a ) , row . member . call ( this , b ) )
58+ } ] ;
59+ if ( row . childLevels ) {
60+ for ( const childRow of row . childLevels ) {
61+ groupingExpressions = groupingExpressions . concat ( this . buildGroupingExpressions ( childRow ) ) ;
62+ }
63+ }
64+ return groupingExpressions ;
65+ }
66+
67+ private applyAggregation ( data : any [ ] , val : IPivotValue ) : void {
68+ for ( const record of data ) {
69+ if ( record . groups ) {
70+ this . applyAggregation ( record . groups , val ) ;
71+ record [ val . member ] = val . aggregate ( record . records . map ( r => r [ val . member ] ) ) ;
72+ } else if ( record . records ) {
73+ record [ val . member ] = val . aggregate ( record . records . map ( r => r [ val . member ] ) ) ;
74+ }
75+ }
2576 }
2677}
2778
2879/**
2980 * @hidden
3081 */
31- @Pipe ( {
82+ @Pipe ( {
3283 name : 'gridPivotColumn' ,
3384 pure : true
3485} )
@@ -40,7 +91,7 @@ export class IgxPivotColumnPipe implements PipeTransform {
4091 collection : any ,
4192 columns : IPivotDimension [ ] ,
4293 values ?: IPivotValue [ ]
43- ) : any [ ] {
94+ ) : any [ ] {
4495 return collection ;
4596 }
4697}
@@ -60,6 +111,19 @@ export class IgxPivotGridFilterPipe implements PipeTransform {
60111 expressionsTree : IFilteringExpressionsTree ,
61112 filterStrategy : IFilteringStrategy ,
62113 advancedExpressionsTree : IFilteringExpressionsTree ) : any [ ] {
63- return collection ;
114+
115+ const state = {
116+ expressionsTree,
117+ strategy : filterStrategy ,
118+ advancedExpressionsTree
119+ } ;
120+
121+ if ( FilteringExpressionsTree . empty ( state . expressionsTree ) && FilteringExpressionsTree . empty ( state . advancedExpressionsTree ) ) {
122+ return collection ;
123+ }
124+
125+ const result = DataUtil . filter ( cloneArray ( collection ) , state ) ;
126+
127+ return result ;
64128 }
65129}
0 commit comments