@@ -18,7 +18,7 @@ import { GridType } from '../common/grid.interface';
1818import { IgxGridNavigationService } from '../grid-navigation.service' ;
1919import { IgxGridCRUDService } from '../common/crud.service' ;
2020import { IgxGridSummaryService } from '../summaries/grid-summary.service' ;
21- import { IPivotConfiguration , IPivotDimension } from './pivot-grid.interface' ;
21+ import { IPivotConfiguration , PivotDimensionType } from './pivot-grid.interface' ;
2222import { IgxPivotHeaderRowComponent } from './pivot-header-row.component' ;
2323import { IgxColumnGroupComponent } from '../columns/column-group.component' ;
2424import { IgxColumnComponent } from '../columns/column.component' ;
@@ -205,26 +205,44 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
205205 return MINIMUM_COLUMN_WIDTH * rowDimCount ;
206206 }
207207
208- public toggleColumn ( col : IgxColumnComponent ) {
209- const state = this . columnGroupStates . get ( col . field ) ;
210- const newState = ! state ;
211- this . columnGroupStates . set ( col . field , newState ) ;
212- const fieldColumns = col . children . filter ( x => ! x . columnGroup ) ;
213- const groupColumns = col . children . filter ( x => x . columnGroup ) ;
214- groupColumns . forEach ( groupColumn => {
208+ public toggleColumn ( col : IgxColumnComponent ) {
209+ const state = this . columnGroupStates . get ( col . field ) ;
210+ const newState = ! state ;
211+ this . columnGroupStates . set ( col . field , newState ) ;
212+ this . toggleGroup ( col , newState ) ;
213+ this . reflow ( ) ;
214+ }
215+
216+ protected toggleGroup ( col : IgxColumnComponent , newState : boolean ) {
217+ if ( this . hasMultipleValues ) {
218+ const fieldColumns = col . children . filter ( x => ! x . columnGroup ) ;
219+ const groupColumns = col . children . filter ( x => x . columnGroup ) ;
220+ groupColumns . forEach ( groupColumn => {
221+ groupColumn . hidden = newState ;
222+ this . resolveToggle ( groupColumn ) ;
223+ } ) ;
224+ fieldColumns . forEach ( fieldColumn => {
225+ fieldColumn . hidden = ! newState ;
226+ } ) ;
227+ } else {
228+ const parentCols = col . parent ? col . parent . children : this . columns . filter ( x => x . level === 0 ) ;
229+ const fieldColumn = parentCols . filter ( x => x . header === col . header && ! x . columnGroup ) [ 0 ] ;
230+ const groupColumn = parentCols . filter ( x => x . header === col . header && x . columnGroup ) [ 0 ] ;
215231 groupColumn . hidden = newState ;
216232 this . resolveToggle ( groupColumn ) ;
217- } ) ;
218- fieldColumns . forEach ( fieldColumn => {
219233 fieldColumn . hidden = ! newState ;
220- } ) ;
221- this . reflow ( ) ;
222- }
234+ if ( newState ) {
235+ fieldColumn . headerTemplate = this . headerTemplate ;
236+ } else {
237+ fieldColumn . headerTemplate = undefined ;
238+ }
239+ }
240+ }
223241
224- protected resolveToggle ( groupColumn : IgxColumnComponent ) {
225- const hasChildGroup = groupColumn . children . filter ( x => x . columnGroup ) . length > 0 ;
226- if ( ! groupColumn . hidden && hasChildGroup ) {
227- const fieldChildren = groupColumn . children . filter ( x => ! x . columnGroup ) ;
242+ protected resolveToggle ( groupColumn : IgxColumnComponent ) {
243+ const hasChildGroup = groupColumn . children . filter ( x => x . columnGroup ) . length > 0 ;
244+ if ( ! groupColumn . hidden && hasChildGroup ) {
245+ const fieldChildren = groupColumn . children . filter ( x => ! x . columnGroup ) ;
228246 const groupChildren = groupColumn . children . filter ( x => x . columnGroup ) ;
229247 groupChildren . forEach ( group => {
230248 this . resolveToggle ( group ) ;
@@ -242,6 +260,10 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
242260 protected calcGridHeadRow ( ) {
243261 }
244262
263+ protected get hasMultipleValues ( ) {
264+ return this . pivotConfiguration . values . length > 1 ;
265+ }
266+
245267 /**
246268 * @hidden
247269 */
@@ -250,8 +272,9 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
250272 const fieldsMap = PivotUtil . getFieldsHierarchy (
251273 data ,
252274 this . pivotConfiguration . columns ,
253- { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
254- ) ;
275+ PivotDimensionType . Column ,
276+ { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
277+ ) ;
255278 const columns = this . generateColumnHierarchy ( fieldsMap , data ) ;
256279 this . _autoGeneratedCols = columns ;
257280
@@ -267,7 +290,9 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
267290 let columns = [ ] ;
268291 fields . forEach ( ( value , key ) => {
269292 if ( value . children == null || value . children . length === 0 || value . children . size === 0 ) {
270- const ref = factoryColumnGroup . create ( this . viewRef . injector ) ;
293+ const ref = this . hasMultipleValues ?
294+ factoryColumnGroup . create ( this . viewRef . injector ) :
295+ factoryColumn . create ( this . viewRef . injector ) ;
271296 ref . instance . header = parent != null ? key . split ( parent . header + '-' ) [ 1 ] : key ;
272297 ref . instance . field = key ;
273298 ref . instance . parent = parent ;
@@ -276,7 +301,12 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
276301 const measureChildren = this . getMeasureChildren ( factoryColumn , data , ref . instance , false ) ;
277302 ref . instance . children . reset ( measureChildren ) ;
278303 columns . push ( ref . instance ) ;
279- columns = columns . concat ( measureChildren ) ;
304+ if ( this . hasMultipleValues ) {
305+ const measureChildren = this . getMeasureChildren ( factoryColumn , data , ref . instance , false ) ;
306+ ref . instance . children . reset ( measureChildren ) ;
307+ columns = columns . concat ( measureChildren ) ;
308+ }
309+
280310 } else {
281311 const ref = factoryColumnGroup . create ( this . viewRef . injector ) ;
282312 ref . instance . parent = parent ;
@@ -285,18 +315,29 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
285315 if ( value . expandable ) {
286316 ref . instance . headerTemplate = this . headerTemplate ;
287317 }
318+ if ( ! this . hasMultipleValues ) {
319+ const refSibling = factoryColumn . create ( this . viewRef . injector ) ;
320+ refSibling . instance . header = parent != null ? key . split ( parent . header + '-' ) [ 1 ] : key ;
321+ refSibling . instance . field = key ;
322+ refSibling . instance . dataType = this . resolveDataTypes ( data [ 0 ] [ key ] ) ;
323+ refSibling . instance . parent = parent ;
324+ refSibling . instance . hidden = true ;
325+ columns . push ( refSibling . instance ) ;
326+ }
288327 const children = this . generateColumnHierarchy ( value . children , data , ref . instance ) ;
289328 const filteredChildren = children . filter ( x => x . level === ref . instance . level + 1 ) ;
290329 ref . changeDetectorRef . detectChanges ( ) ;
291-
292- const measureChildren = this . getMeasureChildren ( factoryColumn , data , ref . instance , true ) ;
293- const nestedChildren = filteredChildren . concat ( measureChildren ) ;
294-
295- const allChildren = children . concat ( measureChildren ) ;
296- ref . instance . children . reset ( nestedChildren ) ;
297-
298330 columns . push ( ref . instance ) ;
299- columns = columns . concat ( allChildren ) ;
331+ if ( this . hasMultipleValues ) {
332+ const measureChildren = this . getMeasureChildren ( factoryColumn , data , ref . instance , true ) ;
333+ const nestedChildren = filteredChildren . concat ( measureChildren ) ;
334+ const allChildren = children . concat ( measureChildren ) ;
335+ ref . instance . children . reset ( nestedChildren ) ;
336+ columns = columns . concat ( allChildren ) ;
337+ } else {
338+ ref . instance . children . reset ( filteredChildren ) ;
339+ columns = columns . concat ( children ) ;
340+ }
300341 }
301342 } ) ;
302343 this . reflow ( ) ;
0 commit comments