Skip to content

Commit b2325ed

Browse files
committed
feat(pivot): Add fieldName as non required param in PivotDimension
1 parent a7e09ea commit b2325ed

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface IPivotDimension {
1515
member: string | ((data: any) => any);
1616
// Enables/Disables a particular dimension from pivot structure.
1717
enabled: boolean;
18+
// additional field name when using member as a function
19+
fieldName?: string;
1820
}
1921

2022
export interface IPivotValue {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Pipe, PipeTransform } from '@angular/core';
2-
import { cloneArray, cloneValue } from '../../core/utils';
2+
import { cloneArray } from '../../core/utils';
33
import { DataUtil } from '../../data-operations/data-util';
44
import { FilteringExpressionsTree, IFilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree';
55
import { IFilteringStrategy } from '../../data-operations/filtering-strategy';
@@ -28,7 +28,7 @@ export class IgxPivotRowPipe implements PipeTransform {
2828
// apply aggregations based on the created groups
2929
PivotUtil.applyAggregations(hierarchies, values, pivotKeys);
3030
// generate flat data from the hierarchies
31-
const data = PivotUtil.flattenHierarchy(hierarchies, collection[0] ?? [], pivotKeys);
31+
const data = PivotUtil.flattenHierarchy(hierarchies, collection[0] ?? [], rows, pivotKeys);
3232
return data;
3333
}
3434
}

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class PivotUtil {
2121
}
2222

2323
public static extractValueFromDimension(dim: IPivotDimension, recData: any) {
24-
return typeof dim.member === 'string' ? recData[dim.member] : dim.member.call(recData);
24+
return typeof dim.member === 'string' ? recData[dim.member] : dim.member.call(null, recData);
2525
}
2626

2727
public static extractValuesFromDimension(dims: IPivotDimension[], recData: any){
@@ -61,21 +61,23 @@ export class PivotUtil {
6161
return result;
6262
}
6363

64-
public static flattenHierarchy(hierarchies, rec, pivotKeys, level = 0) {
64+
public static flattenHierarchy(hierarchies, rec, dims, pivotKeys, level = 0) {
6565
let flatData = [];
66-
const field = this.generateFieldValue(rec);
67-
hierarchies.forEach((h, key) => {
68-
let obj = {};
69-
obj[field] = key;
70-
obj[pivotKeys.records] = h[pivotKeys.records];
71-
obj = {...obj, ...h[pivotKeys.aggregations]};
72-
obj[pivotKeys.level] = level;
73-
flatData.push(obj);
74-
if (h[pivotKeys.children]) {
75-
obj[pivotKeys.records] = this.flattenHierarchy(h[pivotKeys.children], rec, pivotKeys, level + 1);
76-
flatData = [...flatData, ...obj[pivotKeys.records]];
77-
}
78-
});
66+
for (const dim of dims) {
67+
hierarchies.forEach((h, key) => {
68+
const field = (dim && dim.fieldName) ?? this.generateFieldValue(rec);
69+
let obj = {};
70+
obj[field] = key;
71+
obj[pivotKeys.records] = h[pivotKeys.records];
72+
obj = {...obj, ...h[pivotKeys.aggregations]};
73+
obj[pivotKeys.level] = level;
74+
flatData.push(obj);
75+
if (h[pivotKeys.children]) {
76+
obj[pivotKeys.records] = this.flattenHierarchy(h[pivotKeys.children], rec, dim.childLevels, pivotKeys, level + 1);
77+
flatData = [...flatData, ...obj[pivotKeys.records]];
78+
}
79+
});
80+
}
7981

8082
return flatData;
8183
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class PivotGridSampleComponent {
5858
enabled: true,
5959
childLevels:[
6060
{
61+
fieldName: 'ProductCategory',
6162
member: (data) => data.ProductCategory,
6263
enabled: true,
6364
childLevels:[]

0 commit comments

Comments
 (0)