@@ -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,12 +81,12 @@ 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 ;
86+ protected _defaultExpandState = true ;
8587 private _data ;
8688 private _filteredData ;
8789 private p_id = `igx-pivot-grid-${ NEXT_ID ++ } ` ;
88- private _origData = [ ] ;
8990
9091 /**
9192 * @hidden
@@ -150,29 +151,6 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
150151 public get data ( ) : any [ ] | null {
151152 return this . _data ;
152153 }
153-
154- /**
155- * Returns an array of data set to the component.
156- * ```typescript
157- * let processedData = this.grid.processedData;
158- * ```
159- */
160- public get originalData ( ) : any [ ] | null {
161- return this . _origData ;
162- }
163-
164-
165- /**
166- * An @Input property that lets you fill the `IgxPivotGridComponent` with an array of data.
167- * ```html
168- * <igx-pivot-grid [processedData]="Data"></igx-pivot-grid>
169- * ```
170- */
171- @Input ( )
172- public set originalData ( value : any [ ] | null ) {
173- this . _origData = value || [ ] ;
174- }
175-
176154 /**
177155 * Sets an array of objects containing the filtered data.
178156 * ```typescript
@@ -219,16 +197,54 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
219197 }
220198
221199 public toggleColumn ( col : IgxColumnComponent ) {
222- const state = this . columnGroupStates . get ( col ) ;
223- 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+ }
224229 }
225230
231+ /**
232+ * @hidden
233+ * @internal
234+ */
235+ protected calcGridHeadRow ( ) {
236+ }
237+
226238 /**
227239 * @hidden
228240 */
229241 protected autogenerateColumns ( ) {
230242 const data = this . gridAPI . get_data ( ) ;
231- 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+ ) ;
232248 const columns = this . generateColumnHierarchy ( fieldsMap , data ) ;
233249 this . _autoGeneratedCols = columns ;
234250
@@ -243,61 +259,41 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
243259 const factoryColumnGroup = this . resolver . resolveComponentFactory ( IgxColumnGroupComponent ) ;
244260 let columns = [ ] ;
245261 fields . forEach ( ( value , key ) => {
246- if ( value . children == null || value . children . size === 0 ) {
262+ if ( value . children == null || value . children . length === 0 || value . children . size === 0 ) {
247263 const ref = factoryColumn . create ( this . viewRef . injector ) ;
248- ref . instance . header = key ;
264+ ref . instance . header = parent != null ? key . split ( parent . header + '-' ) [ 1 ] : key ;
249265 ref . instance . field = key ;
250266 ref . instance . dataType = this . resolveDataTypes ( data [ 0 ] [ key ] ) ;
251267 ref . instance . parent = parent ;
252268 ref . changeDetectorRef . detectChanges ( ) ;
253269 columns . push ( ref . instance ) ;
254270 } else {
255271 const ref = factoryColumnGroup . create ( this . viewRef . injector ) ;
256- ref . instance . header = key ;
257- 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+ }
258285 const children = this . generateColumnHierarchy ( value . children , data , ref . instance ) ;
286+ const filteredChildren = children . filter ( x => x . level === ref . instance . level + 1 ) ;
259287 ref . changeDetectorRef . detectChanges ( ) ;
260- ref . instance . children . reset ( children ) ;
288+
289+ ref . instance . children . reset ( filteredChildren ) ;
290+
261291 columns . push ( ref . instance ) ;
262292 columns = columns . concat ( children ) ;
263293 }
264294 } ) ;
265295 return columns ;
266296 }
267297
268- protected getFieldsHierarchy ( data ) {
269- const hierarchy = new Map < string , any > ( ) ;
270- for ( const rec of this . originalData ) {
271- const vals = this . extractValuesFromDimension ( this . pivotConfiguration . columns , rec ) ;
272- for ( const val of vals ) {
273- if ( hierarchy . get ( val . value ) != null && val . children ) {
274- for ( const child of val . children ) {
275- hierarchy . get ( val . value ) . children . set ( child . value , child ) ;
276- }
277- } else {
278- hierarchy . set ( val . value , val ) ;
279- hierarchy . get ( val . value ) . children = new Map < string , any > ( ) ;
280- for ( const child of val . children ) {
281- hierarchy . get ( val . value ) . children . set ( child . value , child ) ;
282- }
283- }
284- }
285- }
286- return hierarchy ;
287- }
288298
289- private extractValuesFromDimension ( dims : IPivotDimension [ ] , recData : any ) {
290- const vals = [ ] ;
291- let i = 0 ;
292- for ( const col of dims ) {
293- const value = typeof col . member === 'string' ? recData [ col . member ] : col . member . call ( this , recData ) ;
294- vals . push ( { value } ) ;
295- if ( col . childLevels != null && col . childLevels . length > 0 ) {
296- const childValues = this . extractValuesFromDimension ( col . childLevels , recData ) ;
297- vals [ i ] . children = childValues ;
298- }
299- i ++ ;
300- }
301- return vals ;
302- }
303299}
0 commit comments