Skip to content

Commit f2c4bcc

Browse files
MKirovaMKirova
authored andcommitted
Flatten hierarchy when resolving deeply neested dimensions. Additiona small fixes for toggle resolution and privot row field name resolution.
1 parent a8635f6 commit f2c4bcc

File tree

5 files changed

+66
-46
lines changed

5 files changed

+66
-46
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<igx-icon [attr.draggable]="false"
118118
(click)="toggleColumn(column)"
119119
style='cursor: pointer;'>
120-
{{columnGroupStates.get(column.header) ? 'expand_more' : 'expand_less'}}</igx-icon>
120+
{{columnGroupStates.get(column.field) ? 'expand_more' : 'expand_less'}}</igx-icon>
121121
{{column.header}}
122122
</div>
123123
</ng-template>

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,14 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
196196
}
197197

198198
public toggleColumn(col: IgxColumnComponent) {
199-
const state = this.columnGroupStates.get(col.header);
199+
const state = this.columnGroupStates.get(col.field);
200200
const newState = !state;
201-
this.columnGroupStates.set(col.header, newState);
201+
this.columnGroupStates.set(col.field, newState);
202202
const parentCols = col.parent ? col.parent.children : this.columns.filter(x => x.level === 0);
203203
const fieldColumn = parentCols.filter(x => x.header === col.header && !x.columnGroup)[0];
204204
const groupColumn = parentCols.filter(x => x.header === col.header && x.columnGroup)[0];
205205
groupColumn.hidden = newState;
206-
const hasChildGroup = groupColumn.children.filter(x => x.columnGroup).length > 0;
207-
if (!groupColumn.hidden && hasChildGroup) {
208-
// column group when shown displays all children, we want to show only groups
209-
const fieldChildren = groupColumn.children.filter(x => !x.columnGroup);
210-
fieldChildren.forEach(fieldChild => {
211-
fieldChild.hidden = true;
212-
});
213-
}
206+
this.resolveToggle(groupColumn);
214207
fieldColumn.hidden = !newState;
215208
if (newState) {
216209
fieldColumn.headerTemplate = this.headerTemplate;
@@ -220,6 +213,20 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
220213
this.reflow();
221214
}
222215

216+
protected resolveToggle(groupColumn: IgxColumnComponent) {
217+
const hasChildGroup = groupColumn.children.filter(x => x.columnGroup).length > 0;
218+
if (!groupColumn.hidden && hasChildGroup) {
219+
const fieldChildren = groupColumn.children.filter(x => !x.columnGroup);
220+
const groupChildren = groupColumn.children.filter(x => x.columnGroup);
221+
groupChildren.forEach(group => {
222+
this.resolveToggle(group);
223+
});
224+
fieldChildren.forEach(fieldChild => {
225+
fieldChild.hidden = true;
226+
});
227+
}
228+
}
229+
223230
/**
224231
* @hidden
225232
* @internal
@@ -261,28 +268,26 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
261268
columns.push(ref.instance);
262269
} else {
263270
const ref = factoryColumnGroup.create(this.viewRef.injector);
264-
ref.instance.header = key;
265271
ref.instance.parent = parent;
272+
ref.instance.field = key;
266273
ref.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
267274
if (value.expandable) {
268275
ref.instance.headerTemplate = this.headerTemplate;
276+
const refSibling = factoryColumn.create(this.viewRef.injector);
277+
refSibling.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
278+
refSibling.instance.field = key;
279+
refSibling.instance.dataType = this.resolveDataTypes(data[0][key]);
280+
refSibling.instance.parent = parent;
281+
refSibling.instance.hidden = true;
282+
columns.push(refSibling.instance);
269283
}
270284
const children = this.generateColumnHierarchy(value.children, data, ref.instance);
271-
272-
const refSibling = factoryColumn.create(this.viewRef.injector);
273-
refSibling.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
274-
refSibling.instance.field = key;
275-
refSibling.instance.dataType = this.resolveDataTypes(data[0][key]);
276-
refSibling.instance.parent = parent;
277-
refSibling.instance.hidden = true;
278-
279285
const filteredChildren = children.filter(x => x.level === ref.instance.level + 1);
280286
ref.changeDetectorRef.detectChanges();
281287

282288
ref.instance.children.reset(filteredChildren);
283289

284290
columns.push(ref.instance);
285-
columns.push(refSibling.instance);
286291
columns = columns.concat(children);
287292
}
288293
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ export class IgxPivotRowComponent extends IgxRowDirective<IgxPivotGridComponent>
8585
}
8686

8787
protected _createColComponent(field: string) {
88+
const fieldName = field.indexOf('-') !== -1 ? field.slice(field.lastIndexOf('-') + 1) : field;
8889
const factoryColumn = this.resolver.resolveComponentFactory(IgxColumnComponent);
8990
const ref = this.viewRef.createComponent(factoryColumn, null, this.viewRef.injector);
9091
ref.instance.field = field;
91-
ref.instance.header = field;
92+
ref.instance.header = fieldName;
9293
ref.instance.width = MINIMUM_COLUMN_WIDTH + 'px';
9394
(ref as any).instance._vIndex = this.grid.columns.length + this.index;
9495
ref.instance.headerTemplate = this.headerTemplate;

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,34 @@ export class PivotUtil {
2525

2626
public static extractValuesFromDimension(dims: IPivotDimension[], recData: any, path = []) {
2727
const vals = [];
28-
let lvl = 0;
2928
let lvlCollection = vals;
30-
for (const col of dims) {
29+
const flattenedDims = this.flatten(dims);
30+
for (const col of flattenedDims) {
3131
const value = this.extractValueFromDimension(col, recData);
3232
path.push(value);
3333
const newValue = path.join('-');
3434
lvlCollection.push({ value: newValue });
35-
if (col.childLevels != null && col.childLevels.length > 0) {
36-
const childValues = this.extractValuesFromDimension(col.childLevels, recData, path);
37-
vals[lvl].children = childValues;
38-
vals[lvl].expandable = true;
39-
}
40-
lvl++;
41-
if(!lvlCollection[0].children) {
35+
lvlCollection[0].expandable = col.expandable;
36+
if (!lvlCollection[0].children) {
4237
lvlCollection[0].children = [];
4338
}
4439
lvlCollection = lvlCollection[0].children;
4540
}
4641
return vals;
4742
}
4843

44+
public static flatten(arr) {
45+
const newArr = arr.reduce((acc, item) => {
46+
acc.push(item);
47+
if (Array.isArray(item.childLevels) && item.childLevels.length > 0) {
48+
item.expandable = true;
49+
acc = acc.concat(this.flatten(item.childLevels));
50+
}
51+
return acc;
52+
}, []);
53+
return newArr;
54+
}
55+
4956
public static applyAggregations(hierarchies, values, pivotKeys) {
5057
hierarchies.forEach((hierarchy) => {
5158
const children = hierarchy[pivotKeys.children];

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,28 @@ export class PivotGridSampleComponent {
4545

4646
public pivotConfigHierarchy: IPivotConfiguration = {
4747
columns: [{
48-
member: 'Country',
49-
enabled: true,
50-
childLevels:[]
51-
},
52-
{
53-
member: 'SellerName',
54-
enabled: true,
55-
childLevels:[]
56-
},
57-
{
58-
member: 'Date',
59-
enabled: true,
60-
childLevels: []
61-
}
62-
],
48+
member: 'Country',
49+
enabled: true,
50+
childLevels: [{
51+
member: 'SellerName',
52+
enabled: true,
53+
childLevels: [
54+
{
55+
member: 'Date',
56+
enabled: true,
57+
childLevels: []
58+
},
59+
]
60+
}]
61+
},
62+
63+
{
64+
member: 'ProductCategory',
65+
enabled: true,
66+
childLevels: []
67+
}
68+
]
69+
,
6370
rows: [{
6471
member: () => 'All',
6572
enabled: true,

0 commit comments

Comments
 (0)