Skip to content

Commit aa170fb

Browse files
MKirovaMKirova
authored andcommitted
Add levels display and handling for column dimension.
1 parent ba86e93 commit aa170fb

File tree

5 files changed

+106
-17
lines changed

5 files changed

+106
-17
lines changed

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,9 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
28312831
public summaryPipeTrigger = 0;
28322832

28332833
public EMPTY_DATA = [];
2834+
2835+
public isPivot = false;
2836+
28342837
/**
28352838
* @hidden
28362839
*/
@@ -2922,6 +2925,7 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
29222925
protected _userOutletDirective: IgxOverlayOutletDirective;
29232926
protected _transactions: TransactionService<Transaction, State>;
29242927
protected _batchEditing = false;
2928+
protected _autoGeneratedCols = [];
29252929

29262930
/** @hidden @internal */
29272931
public get paginator() {
@@ -2994,7 +2998,6 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
29942998
private _unpinnedWidth = NaN;
29952999
private _visibleColumns = [];
29963000
private _columnGroups = false;
2997-
private _autoGeneratedCols = [];
29983001

29993002
private _columnWidth: string;
30003003

projects/igniteui-angular/src/lib/grids/headers/grid-header.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class IgxGridHeaderComponent implements DoCheck, OnDestroy {
117117

118118
@HostBinding('style.height.rem')
119119
public get height() {
120-
if (!this.grid.hasColumnGroups) {
120+
if (!this.grid.hasColumnGroups || this.grid.isPivot) {
121121
return null;
122122
}
123123

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,13 @@
110110
<igx-grid-column-resizer *ngIf="colResizingService.showResizer"></igx-grid-column-resizer>
111111
<div class="igx-grid__loading-outlet" #igxLoadingOverlayOutlet igxOverlayOutlet></div>
112112
<div class="igx-grid__outlet" #igxFilteringOverlayOutlet igxOverlayOutlet></div>
113+
114+
<ng-template #headerTemplate let-column>
115+
<div class="igx-grid__tr--header">
116+
<igx-icon [attr.draggable]="false"
117+
(click)="toggleColumn(column)"
118+
style='cursor: pointer;'>
119+
{{columnGroupStates.get(column) ? 'expand_more' : 'expand_less'}}</igx-icon>
120+
{{column.header}}
121+
</div>
122+
</ng-template>

projects/igniteui-angular/src/lib/grids/pivot-grid/pivot-grid.component.ts

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { IgxGridCRUDService } from '../common/crud.service';
1919
import { IgxGridSummaryService } from '../summaries/grid-summary.service';
2020
import { IPivotConfiguration, IPivotDimension } from './pivot-grid.interface';
2121
import { IgxPivotHeaderRowComponent } from './pivot-header-row.component';
22+
import { IgxColumnGroupComponent } from '../columns/column-group.component';
23+
import { IgxColumnComponent } from '../columns/column.component';
2224

2325
let NEXT_ID = 0;
2426
const 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
}

src/app/pivot-grid/pivot-grid.sample.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ export class PivotGridSampleComponent {
3535

3636
public pivotConfigHierarchy: IPivotConfiguration = {
3737
columns: [{
38-
member: 'Country',
38+
member: () => 'All',
3939
enabled: true,
40-
childLevels:[]
40+
childLevels: [{
41+
member: 'Country',
42+
enabled: true,
43+
childLevels:[]
44+
}]
4145
}],
4246
rows: [{
4347
member: () => 'All',
@@ -79,15 +83,15 @@ export class PivotGridSampleComponent {
7983
];
8084

8185
public dataHierarchical = [
82-
{ ProductCategory: 'All', Bulgaria: 774, USA: 829, Uruguay: 524, level: 0, records: [
86+
{ ProductCategory: 'All', All: 1000, Bulgaria: 774, USA: 829, Uruguay: 524, level: 0, records: [
8387
{ ProductCategory: 'Clothing', Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
8488
{ ProductCategory: 'Bikes', Uruguay: 68, level: 1 },
8589
{ ProductCategory: 'Accessories', USA: 293, level: 1 },
8690
{ ProductCategory: 'Components', USA: 240, level: 1 }
8791
] },
88-
{ ProductCategory: 'Clothing', Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
89-
{ ProductCategory: 'Bikes', Uruguay: 68, level: 1 },
90-
{ ProductCategory: 'Accessories', USA: 293, level: 1 },
91-
{ ProductCategory: 'Components', USA: 240, level: 1 }
92+
{ ProductCategory: 'Clothing', All: 1000, Bulgaria: 774, USA: 296, Uruguay: 456, level: 1 },
93+
{ ProductCategory: 'Bikes', All: 1000, Uruguay: 68, level: 1 },
94+
{ ProductCategory: 'Accessories', All: 1000, USA: 293, level: 1 },
95+
{ ProductCategory: 'Components', All: 1000, USA: 240, level: 1 }
9296
];
9397
}

0 commit comments

Comments
 (0)