@@ -19,6 +19,8 @@ import { IgxGridCRUDService } from '../common/crud.service';
1919import { IgxGridSummaryService } from '../summaries/grid-summary.service' ;
2020import { IPivotConfiguration , IPivotDimension } from './pivot-grid.interface' ;
2121import { IgxPivotHeaderRowComponent } from './pivot-header-row.component' ;
22+ import { IgxColumnGroupComponent } from '../columns/column-group.component' ;
23+ import { IgxColumnComponent } from '../columns/column.component' ;
2224
2325let NEXT_ID = 0 ;
2426const MINIMUM_COLUMN_WIDTH = 200 ;
@@ -43,6 +45,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
4345 GridType {
4446
4547
48+
4649 /** @hidden @internal */
4750 @ViewChild ( IgxPivotHeaderRowComponent , { static : true } )
4851 public theadRow : IgxPivotHeaderRowComponent ;
@@ -70,6 +73,15 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
7073 @ViewChild ( 'record_template' , { read : TemplateRef , static : true } )
7174 public recordTemplate : TemplateRef < any > ;
7275
76+ /**
77+ * @hidden @internal
78+ */
79+ @ViewChild ( 'headerTemplate' , { read : TemplateRef , static : true } )
80+ public headerTemplate : TemplateRef < any > ;
81+
82+
83+ public columnGroupStates = new Map < IgxColumnGroupComponent , boolean > ( ) ;
84+ public isPivot = true ;
7385 private _data ;
7486 private _filteredData ;
7587 private p_id = `igx-pivot-grid-${ NEXT_ID ++ } ` ;
@@ -206,25 +218,85 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
206218 return MINIMUM_COLUMN_WIDTH * rowDimCount ;
207219 }
208220
209- // TODO: should work on original data, before pipes.
210- // That should be data, but for this poc since we have no pipes use a different prop.
211- protected generateDataFields ( data : any [ ] ) : string [ ] {
212- let fields = new Map < string , string > ( ) ;
221+ public toggleColumn ( col : IgxColumnComponent ) {
222+ const state = this . columnGroupStates . get ( col ) ;
223+ this . columnGroupStates . set ( col , ! state ) ;
224+ }
225+
226+ /**
227+ * @hidden
228+ */
229+ protected autogenerateColumns ( ) {
230+ const data = this . gridAPI . get_data ( ) ;
231+ const fieldsMap = this . getFieldsHierarchy ( data ) ;
232+ const columns = this . generateColumnHierarchy ( fieldsMap , data ) ;
233+ this . _autoGeneratedCols = columns ;
234+
235+ this . columnList . reset ( columns ) ;
236+ if ( data && data . length > 0 ) {
237+ this . shouldGenerate = false ;
238+ }
239+ }
240+
241+ protected generateColumnHierarchy ( fields : Map < string , any > , data , parent = null ) : IgxColumnComponent [ ] {
242+ const factoryColumn = this . resolver . resolveComponentFactory ( IgxColumnComponent ) ;
243+ const factoryColumnGroup = this . resolver . resolveComponentFactory ( IgxColumnGroupComponent ) ;
244+ let columns = [ ] ;
245+ fields . forEach ( ( value , key ) => {
246+ if ( value . children == null ) {
247+ const ref = factoryColumn . create ( this . viewRef . injector ) ;
248+ ref . instance . header = key ;
249+ ref . instance . field = key ;
250+ ref . instance . dataType = this . resolveDataTypes ( data [ 0 ] [ key ] ) ;
251+ ref . instance . parent = parent ;
252+ ref . changeDetectorRef . detectChanges ( ) ;
253+ columns . push ( ref . instance ) ;
254+ } else {
255+ const ref = factoryColumnGroup . create ( this . viewRef . injector ) ;
256+ ref . instance . header = key ;
257+ ref . instance . headerTemplate = this . headerTemplate ;
258+ const children = this . generateColumnHierarchy ( value . children , data , ref . instance ) ;
259+ ref . changeDetectorRef . detectChanges ( ) ;
260+ ref . instance . children . reset ( children ) ;
261+ columns . push ( ref . instance ) ;
262+ columns = columns . concat ( children ) ;
263+ }
264+ } ) ;
265+ return columns ;
266+ }
267+
268+ protected getFieldsHierarchy ( data ) {
269+ const hierarchy = new Map < string , any > ( ) ;
213270 for ( const rec of this . originalData ) {
214271 const vals = this . extractValuesFromDimension ( this . pivotConfiguration . columns , rec ) ;
215272 for ( const val of vals ) {
216- fields = fields . set ( val , val ) ;
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+ }
217284 }
218285 }
219- const keys = Array . from ( fields . keys ( ) ) ;
220- return keys ;
286+ return hierarchy ;
221287 }
222288
223289 private extractValuesFromDimension ( dims : IPivotDimension [ ] , recData : any ) {
224290 const vals = [ ] ;
291+ let i = 0 ;
225292 for ( const col of dims ) {
226293 const value = typeof col . member === 'string' ? recData [ col . member ] : col . member . call ( this , recData ) ;
227- vals . push ( value ) ;
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 ++ ;
228300 }
229301 return vals ;
230302 }
0 commit comments