Skip to content

Commit 54eb4a1

Browse files
MKirovaMKirova
authored andcommitted
Allow setting custom pivot keys via the configuration. Update all hardcoded keys.
1 parent 3cff889 commit 54eb4a1

File tree

8 files changed

+41
-36
lines changed

8 files changed

+41
-36
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { IGridGroupingStrategy, IGridSortingStrategy } from './strategy';
3434
import { IForOfState, IgxGridForOfDirective } from '../../directives/for-of/for_of.directive';
3535
import { OverlaySettings } from '../../services/overlay/utilities';
3636
import { IPinningConfig } from '../grid.common';
37-
import { IPivotConfiguration, IPivotDimension, IPivotValue } from '../pivot-grid/pivot-grid.interface';
37+
import { IPivotConfiguration, IPivotDimension, IPivotKeys, IPivotValue } from '../pivot-grid/pivot-grid.interface';
3838

3939

4040
export const IGX_GRID_BASE = new InjectionToken<GridType>('IgxGridBaseToken');
@@ -628,6 +628,7 @@ export interface PivotGridType extends GridType {
628628
setupColumns(): void;
629629
toggleRow(rowID: any): void;
630630
resolveDataTypes(field: any): GridColumnDataType;
631+
pivotKeys: IPivotKeys;
631632
}
632633
export interface GridSVGIcon {
633634
name: string;

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
@@ -34,7 +34,7 @@
3434
| pivotGridSort:pivotConfiguration:sortStrategy:id:pipeTrigger
3535
| pivotGridRow:pivotConfiguration:expansionStates:pipeTrigger:sortingExpressions
3636
| pivotGridColumn:pivotConfiguration:expansionStates:pipeTrigger:sortingExpressions
37-
| pivotGridColumnSort:sortingExpressions:sortStrategy:pipeTrigger
37+
| pivotGridColumnSort:sortingExpressions:sortStrategy:pipeTrigger:pivotConfiguration.pivotKeys
3838
| pivotGridRowExpansion:pivotConfiguration:expansionStates:pipeTrigger"
3939
let-rowIndex="index" [igxForScrollOrientation]="'vertical'" [igxForScrollContainer]='verticalScroll'
4040
[igxForContainerSize]='calcHeight'

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { IgxForOfSyncService, IgxForOfScrollSyncService } from '../../directives
3333
import { GridServiceType, GridType, IGX_GRID_BASE, IGX_GRID_SERVICE_BASE, RowType } from '../common/grid.interface';
3434
import { IgxGridCRUDService } from '../common/crud.service';
3535
import { IgxGridSummaryService } from '../summaries/grid-summary.service';
36-
import { IPivotConfiguration, IPivotDimension, IPivotKeys, PivotDimensionType } from './pivot-grid.interface';
36+
import { IPivotConfiguration, IPivotDimension, PivotDimensionType } from './pivot-grid.interface';
3737
import { IgxPivotHeaderRowComponent } from './pivot-header-row.component';
3838
import { IgxColumnGroupComponent } from '../columns/column-group.component';
3939
import { IgxColumnComponent } from '../columns/column.component';
@@ -266,7 +266,9 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
266266

267267
public columnGroupStates = new Map<string, boolean>();
268268
public dimensionDataColumns;
269-
public pivotKeys: IPivotKeys = {aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'};
269+
public get pivotKeys() {
270+
return this.pivotConfiguration.pivotKeys || {aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'};
271+
}
270272
public isPivot = true;
271273

272274
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface IPivotConfiguration {
3131
values: IPivotValue[] | null;
3232
// dimensions to be displayed in the filter area.
3333
filters?: IPivotDimension[] | null;
34+
pivotKeys?: IPivotKeys;
3435
}
3536

3637
export interface IPivotDimension {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export class IgxPivotRowPipe implements PipeTransform {
2929
config: IPivotConfiguration,
3030
_: Map<any, boolean>,
3131
_pipeTrigger?: number,
32-
__?,
33-
pivotKeys: IPivotKeys = { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' }
32+
__?
3433
): any[] {
34+
const pivotKeys = config.pivotKeys || { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' };
3535
const enabledRows = config.rows.filter(x => x.enabled);
3636
const rowStrategy = config.rowStrategy || PivotRowDimensionsStrategy.instance();
3737
const data = cloneArray(collection, true);
@@ -56,8 +56,8 @@ export class IgxPivotRowExpansionPipe implements PipeTransform {
5656
expansionStates: Map<any, boolean>,
5757
_pipeTrigger?: number,
5858
__?,
59-
pivotKeys: IPivotKeys = { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' }
6059
): any[] {
60+
const pivotKeys = config.pivotKeys || { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' };
6161
const enabledRows = config.rows.filter(x => x.enabled);
6262
const data = collection ? cloneArray(collection, true) : [];
6363
let totalLlv = 0;
@@ -100,9 +100,9 @@ export class IgxPivotColumnPipe implements PipeTransform {
100100
config: IPivotConfiguration,
101101
_: Map<any, boolean>,
102102
_pipeTrigger?: number,
103-
__?,
104-
pivotKeys: IPivotKeys = { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' }
103+
__?
105104
): any[] {
105+
const pivotKeys = config.pivotKeys || { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' };
106106
const enabledColumns = config.columns.filter(x => x.enabled);
107107
const enabledValues = config.values.filter(x => x.enabled);
108108

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class IgxPivotRowComponent extends IgxRowDirective implements OnChanges {
7171
*/
7272
public getRowDimensionKey(col: IgxColumnComponent) {
7373
const dimData = this.rowDimensionData.find(x => x.column === col);
74-
const key = PivotUtil.getRecordKey(this.data, dimData.dimension, dimData.prevDimensions);
74+
const key = PivotUtil.getRecordKey(this.data, dimData.dimension, dimData.prevDimensions, this.grid.pivotKeys);
7575
return key;
7676
}
7777

@@ -110,8 +110,7 @@ export class IgxPivotRowComponent extends IgxRowDirective implements OnChanges {
110110
currentLvl += level;
111111
const prev = [];
112112
for (const dim of rowDimConfig) {
113-
const dimData = PivotUtil.getDimensionLevel(dim, this.data,
114-
{ aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'});
113+
const dimData = PivotUtil.getDimensionLevel(dim, this.data, this.grid.pivotKeys);
115114
dimIndex += dimData.level;
116115
currentLvl += dimData.level;
117116
const column = this.extractFromDimension(dimData.dimension, dimIndex, currentLvl);
@@ -137,9 +136,9 @@ export class IgxPivotRowComponent extends IgxRowDirective implements OnChanges {
137136
ref.instance.header = header;
138137
ref.instance.width = MINIMUM_COLUMN_WIDTH + 'px';
139138
(ref as any).instance._vIndex = this.grid.columns.length + index + this.index * this.grid.pivotConfiguration.rows.length;
140-
if (dim.childLevel && lvl >= PivotUtil.getTotalLvl(this.data)) {
139+
if (dim.childLevel && lvl >= PivotUtil.getTotalLvl(this.data, this.grid.pivotKeys)) {
141140
ref.instance.headerTemplate = this.headerTemplate;
142-
} else if (lvl < PivotUtil.getTotalLvl(this.data)) {
141+
} else if (lvl < PivotUtil.getTotalLvl(this.data, this.grid.pivotKeys)) {
143142
ref.instance.headerTemplate = this.headerTemplateGray;
144143
}
145144
return ref.instance;

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export class PivotUtil {
1414
this.extractValuesForRow(dimensions, rec, pivotKeys);
1515
for (const [key, val] of vals) { // this should go in depth also vals.children
1616
if (hierarchy.get(val.value) != null) {
17-
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
17+
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys);
1818
} else {
1919
hierarchy.set(val.value, cloneValue(val));
20-
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys.records);
20+
this.applyHierarchyChildren(hierarchy, val, rec, pivotKeys);
2121
}
2222
}
2323
}
@@ -79,7 +79,7 @@ export class PivotUtil {
7979
continue;
8080
}
8181
rec[field + '_' + pivotKeys.level] = currDimLvl;
82-
const expansionRowKey = PivotUtil.getRecordKey(rec, dim, prevDims);
82+
const expansionRowKey = PivotUtil.getRecordKey(rec, dim, prevDims, pivotKeys);
8383
const isExpanded = expansionStates.get(expansionRowKey) === undefined ?
8484
defaultExpandState :
8585
expansionStates.get(expansionRowKey);
@@ -219,7 +219,7 @@ export class PivotUtil {
219219
}
220220
for (const property in parentRec) {
221221
if (parentRec.hasOwnProperty(property) &&
222-
Object.keys(pivotKeys).indexOf(property) === -1) {
222+
Object.values(pivotKeys).indexOf(property) === -1) {
223223
siblingData.forEach(s => {
224224
s[property] = parentRec[property];
225225
});
@@ -313,23 +313,22 @@ export class PivotUtil {
313313
return leafs;
314314
}
315315

316-
public static getRecordKey(rec, currentDim: IPivotDimension, prevDims: IPivotDimension[]) {
316+
public static getRecordKey(rec, currentDim: IPivotDimension, prevDims: IPivotDimension[], pivotKeys: IPivotKeys) {
317317
const parentFields = [];
318318
const field = currentDim.memberName;
319319
const value = rec[field];
320320
for(const prev of prevDims) {
321-
const dimData = PivotUtil.getDimensionLevel(prev, rec,
322-
{ aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'});
321+
const dimData = PivotUtil.getDimensionLevel(prev, rec, pivotKeys);
323322
parentFields.push(rec[dimData.dimension.memberName]);
324323
}
325324
parentFields.push(value);
326325
return parentFields.join('_');
327326
}
328327

329-
public static getTotalLvl(rec) {
328+
public static getTotalLvl(rec, pivotKeys: IPivotKeys) {
330329
let total = 0;
331330
Object.keys(rec).forEach(key => {
332-
if (key.indexOf('_level') !== -1 && key.indexOf('level_') === -1 && key.indexOf('records') === -1) {
331+
if (key.indexOf('_' + pivotKeys.level) !== -1 && key.indexOf(pivotKeys.level + '_') === -1 && key.indexOf(pivotKeys.records) === -1) {
333332
total += rec[key] || 0;
334333
}
335334
});
@@ -374,10 +373,12 @@ export class PivotUtil {
374373
return result;
375374
}
376375

377-
private static applyHierarchyChildren(hierarchy, val, rec, recordsKey) {
378-
const childCollection = val.children;
379-
if (Array.isArray(hierarchy.get(val.value).children)) {
380-
hierarchy.get(val.value).children = new Map<string, any>();
376+
private static applyHierarchyChildren(hierarchy, val, rec, pivotKeys: IPivotKeys) {
377+
const recordsKey = pivotKeys.records;
378+
const childKey = pivotKeys.children;
379+
const childCollection = val[childKey];
380+
if (Array.isArray(hierarchy.get(val.value)[childKey])) {
381+
hierarchy.get(val.value)[childKey] = new Map<string, any>();
381382
}
382383
if (!childCollection || childCollection.size === 0) {
383384
const dim = hierarchy.get(val.value).dimension;
@@ -391,26 +392,26 @@ export class PivotUtil {
391392
}
392393
} else {
393394
for (const [key, child] of childCollection) {
394-
if (!hierarchy.get(val.value).children.get(child.value)) {
395-
hierarchy.get(val.value).children.set(child.value, child);
395+
if (!hierarchy.get(val.value)[childKey].get(child.value)) {
396+
hierarchy.get(val.value)[childKey].set(child.value, child);
396397
}
397398

398-
if (hierarchy.get(val.value).children.get(child.value)[recordsKey]) {
399+
if (hierarchy.get(val.value)[childKey].get(child.value)[recordsKey]) {
399400
const copy = Object.assign({}, rec);
400401
if (rec[recordsKey]) {
401402
// not all nested children are valid
402-
const nestedValue = hierarchy.get(val.value).children.get(child.value).value;
403-
const dimension = hierarchy.get(val.value).children.get(child.value).dimension;
403+
const nestedValue = hierarchy.get(val.value)[childKey].get(child.value).value;
404+
const dimension = hierarchy.get(val.value)[childKey].get(child.value).dimension;
404405
const validRecs = rec[recordsKey].filter(x => this.extractValueFromDimension(dimension, x) === nestedValue);
405406
copy[recordsKey] = validRecs;
406407
}
407-
hierarchy.get(val.value).children.get(child.value)[recordsKey].push(copy);
408+
hierarchy.get(val.value)[childKey].get(child.value)[recordsKey].push(copy);
408409
} else {
409-
hierarchy.get(val.value).children.get(child.value)[recordsKey] = [rec];
410+
hierarchy.get(val.value)[childKey].get(child.value)[recordsKey] = [rec];
410411
}
411412

412-
if (child.children && child.children.size > 0) {
413-
this.applyHierarchyChildren(hierarchy.get(val.value).children, child, rec, recordsKey);
413+
if (child[childKey] && child[childKey].size > 0) {
414+
this.applyHierarchyChildren(hierarchy.get(val.value)[childKey], child, rec, pivotKeys);
414415
}
415416
}
416417
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class PivotGridSampleComponent {
4343
@ViewChild('grid1', { static: true }) public grid1: IgxPivotGridComponent;
4444

4545
public pivotConfigHierarchy: IPivotConfiguration = {
46+
pivotKeys: { aggregations: 'aggregationsVals', records: 'values', children: 'childrenCollection', level: 'lvl'},
4647
columns: [
4748
{
4849
memberName: 'Country',

0 commit comments

Comments
 (0)