@@ -21,6 +21,7 @@ import { IPivotConfiguration, IPivotDimension } from './pivot-grid.interface';
2121import { IgxPivotHeaderRowComponent } from './pivot-header-row.component' ;
2222import { IgxColumnGroupComponent } from '../columns/column-group.component' ;
2323import { IgxColumnComponent } from '../columns/column.component' ;
24+ import { PivotUtil } from './pivot-util' ;
2425
2526let NEXT_ID = 0 ;
2627const MINIMUM_COLUMN_WIDTH = 200 ;
@@ -80,7 +81,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
8081 public headerTemplate : TemplateRef < any > ;
8182
8283
83- public columnGroupStates = new Map < IgxColumnGroupComponent , boolean > ( ) ;
84+ public columnGroupStates = new Map < string , boolean > ( ) ;
8485 public isPivot = true ;
8586 protected _defaultExpandState = true ;
8687 private _data ;
@@ -196,16 +197,54 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
196197 }
197198
198199 public toggleColumn ( col : IgxColumnComponent ) {
199- const state = this . columnGroupStates . get ( col ) ;
200- this . columnGroupStates . set ( col , ! state ) ;
200+ const state = this . columnGroupStates . get ( col . field ) ;
201+ const newState = ! state ;
202+ this . columnGroupStates . set ( col . field , newState ) ;
203+ const parentCols = col . parent ? col . parent . children : this . columns . filter ( x => x . level === 0 ) ;
204+ const fieldColumn = parentCols . filter ( x => x . header === col . header && ! x . columnGroup ) [ 0 ] ;
205+ const groupColumn = parentCols . filter ( x => x . header === col . header && x . columnGroup ) [ 0 ] ;
206+ groupColumn . hidden = newState ;
207+ this . resolveToggle ( groupColumn ) ;
208+ fieldColumn . hidden = ! newState ;
209+ if ( newState ) {
210+ fieldColumn . headerTemplate = this . headerTemplate ;
211+ } else {
212+ fieldColumn . headerTemplate = undefined ;
213+ }
214+ this . reflow ( ) ;
215+ }
216+
217+ protected resolveToggle ( groupColumn : IgxColumnComponent ) {
218+ const hasChildGroup = groupColumn . children . filter ( x => x . columnGroup ) . length > 0 ;
219+ if ( ! groupColumn . hidden && hasChildGroup ) {
220+ const fieldChildren = groupColumn . children . filter ( x => ! x . columnGroup ) ;
221+ const groupChildren = groupColumn . children . filter ( x => x . columnGroup ) ;
222+ groupChildren . forEach ( group => {
223+ this . resolveToggle ( group ) ;
224+ } ) ;
225+ fieldChildren . forEach ( fieldChild => {
226+ fieldChild . hidden = true ;
227+ } ) ;
228+ }
201229 }
202230
231+ /**
232+ * @hidden
233+ * @internal
234+ */
235+ protected calcGridHeadRow ( ) {
236+ }
237+
203238 /**
204239 * @hidden
205240 */
206241 protected autogenerateColumns ( ) {
207242 const data = this . gridAPI . get_data ( ) ;
208- const fieldsMap = this . getFieldsHierarchy ( data ) ;
243+ const fieldsMap = PivotUtil . getFieldsHierarchy (
244+ data ,
245+ this . pivotConfiguration . columns ,
246+ { aggregations : 'aggregations' , records : 'records' , children : 'children' , level : 'level' }
247+ ) ;
209248 const columns = this . generateColumnHierarchy ( fieldsMap , data ) ;
210249 this . _autoGeneratedCols = columns ;
211250
@@ -220,33 +259,34 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
220259 const factoryColumnGroup = this . resolver . resolveComponentFactory ( IgxColumnGroupComponent ) ;
221260 let columns = [ ] ;
222261 fields . forEach ( ( value , key ) => {
223- if ( value . children == null || value . children . size === 0 ) {
262+ if ( value . children == null || value . children . length === 0 || value . children . size === 0 ) {
224263 const ref = factoryColumn . create ( this . viewRef . injector ) ;
225- ref . instance . header = key ;
264+ ref . instance . header = parent != null ? key . split ( parent . header + '-' ) [ 1 ] : key ;
226265 ref . instance . field = key ;
227266 ref . instance . dataType = this . resolveDataTypes ( data [ 0 ] [ key ] ) ;
228267 ref . instance . parent = parent ;
229- ref . instance . visibleWhenCollapsed = false ;
230268 ref . changeDetectorRef . detectChanges ( ) ;
231269 columns . push ( ref . instance ) ;
232270 } else {
233271 const ref = factoryColumnGroup . create ( this . viewRef . injector ) ;
234- ref . instance . header = key ;
235- //ref.instance.headerTemplate = this.headerTemplate;
272+ ref . instance . parent = parent ;
273+ ref . instance . field = key ;
274+ ref . instance . header = parent != null ? key . split ( parent . header + '-' ) [ 1 ] : key ;
275+ if ( value . expandable ) {
276+ ref . instance . headerTemplate = this . headerTemplate ;
277+ const refSibling = factoryColumn . create ( this . viewRef . injector ) ;
278+ refSibling . instance . header = parent != null ? key . split ( parent . header + '-' ) [ 1 ] : key ;
279+ refSibling . instance . field = key ;
280+ refSibling . instance . dataType = this . resolveDataTypes ( data [ 0 ] [ key ] ) ;
281+ refSibling . instance . parent = parent ;
282+ refSibling . instance . hidden = true ;
283+ columns . push ( refSibling . instance ) ;
284+ }
236285 const children = this . generateColumnHierarchy ( value . children , data , ref . instance ) ;
237-
238- const refChild = factoryColumn . create ( this . viewRef . injector ) ;
239- refChild . instance . header = key ;
240- refChild . instance . field = key ;
241- refChild . instance . dataType = this . resolveDataTypes ( data [ 0 ] [ key ] ) ;
242- refChild . instance . parent = ref . instance ;
243- refChild . instance . visibleWhenCollapsed = true ;
244-
245- children . push ( refChild . instance ) ;
246-
286+ const filteredChildren = children . filter ( x => x . level === ref . instance . level + 1 ) ;
247287 ref . changeDetectorRef . detectChanges ( ) ;
248- ref . instance . children . reset ( children ) ;
249- ref . instance . collapsible = true ;
288+
289+ ref . instance . children . reset ( filteredChildren ) ;
250290
251291 columns . push ( ref . instance ) ;
252292 columns = columns . concat ( children ) ;
@@ -255,48 +295,5 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
255295 return columns ;
256296 }
257297
258- protected getFieldsHierarchy ( data ) {
259- const hierarchy = new Map < string , any > ( ) ;
260- for ( const rec of data ) {
261- const vals = this . extractValuesFromDimension ( this . pivotConfiguration . columns , rec ) ;
262- for ( const val of vals ) {
263- if ( hierarchy . get ( val . value ) != null && val . children ) {
264- for ( const child of val . children ) {
265- hierarchy . get ( val . value ) . children . set ( child . value , child ) ;
266- }
267- } else {
268- const children = val . children ;
269- hierarchy . set ( val . value , val ) ;
270- hierarchy . get ( val . value ) . children = new Map < string , any > ( ) ;
271- for ( const child of children ) {
272- hierarchy . get ( val . value ) . children . set ( child . value , child ) ;
273- }
274- }
275- }
276- }
277- return hierarchy ;
278- }
279298
280- private extractValuesFromDimension ( dims : IPivotDimension [ ] , recData : any ) {
281- const vals = [ ] ;
282- let i = 0 ;
283- for ( const col of dims ) {
284- const value = typeof col . member === 'string' ? recData [ col . member ] : col . member . call ( this , recData ) ;
285- if ( vals . length > 0 ) {
286- const newChildVal = vals [ 0 ] . value + '-' + value ;
287- if ( ! vals [ 0 ] . children ) {
288- vals [ 0 ] . children = [ ] ;
289- }
290- vals [ 0 ] . children . push ( { value } ) ;
291- } else {
292- vals . push ( { value } ) ;
293- }
294- if ( col . childLevels != null && col . childLevels . length > 0 ) {
295- const childValues = this . extractValuesFromDimension ( col . childLevels , recData ) ;
296- vals [ i ] . children = childValues ;
297- }
298- i ++ ;
299- }
300- return vals ;
301- }
302299}
0 commit comments