Skip to content

Commit a6a744a

Browse files
committed
refactor(pivot-grid): remove repetitive code
1 parent 038a431 commit a6a744a

File tree

4 files changed

+30
-48
lines changed

4 files changed

+30
-48
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ export class IgxPivotDataSelectorComponent {
523523
this.value.aggregate = event.newSelection.value;
524524
const isSingleValue = this.grid.values.length === 1;
525525

526-
PivotUtil.handleCountAggregator(this.grid.columns, this.value, isSingleValue);
526+
PivotUtil.updateColumnTypeByAggregator(this.grid.columns, this.value, isSingleValue);
527527

528528
this.grid.pipeTrigger++;
529529
this.grid.cdr.markForCheck();

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,23 +2274,6 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
22742274
});
22752275
return hierarchy;
22762276
}
2277-
private updateColumnDataTypeByAggregator(column: any) {
2278-
this.values.forEach((aggregatorValue) => {
2279-
if ((aggregatorValue.dataType === GridColumnDataType.Currency || aggregatorValue.dataType === GridColumnDataType.Percent) && aggregatorValue.aggregate?.key?.toLowerCase() === 'count') {
2280-
if (column.field?.includes(aggregatorValue.member) || this.values.length === 1) {
2281-
column.dataType = GridColumnDataType.Number;
2282-
}
2283-
} else if (aggregatorValue.dataType === GridColumnDataType.Currency && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2284-
if (column.field?.includes(aggregatorValue.member) || this.values.length === 1) {
2285-
column.dataType = GridColumnDataType.Currency;
2286-
}
2287-
} else if (aggregatorValue.dataType === GridColumnDataType.Percent && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2288-
if (column.field?.includes(aggregatorValue.member) || this.values.length === 1) {
2289-
column.dataType = GridColumnDataType.Percent;
2290-
}
2291-
}
2292-
})
2293-
}
22942277
protected generateColumnHierarchy(fields: Map<string, any>, data, parent = null): IgxColumnComponent[] {
22952278
let columns = [];
22962279
if (fields.size === 0) {
@@ -2321,15 +2304,18 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
23212304
if (shouldGenerate && (value.children == null || value.children.length === 0 || value.children.size === 0)) {
23222305
const col = this.createColumnForDimension(value, data, parent, this.hasMultipleValues);
23232306

2324-
this.updateColumnDataTypeByAggregator(col);
2307+
if (!this.hasMultipleValues && this.values.length > 0) {
2308+
PivotUtil.updateColumnTypeByAggregator([col], this.values[0], true);
2309+
}
23252310

23262311
columns.push(col);
23272312
if (this.hasMultipleValues) {
23282313
const measureChildren = this.getMeasureChildren(data, col, false, value.dimension.width);
23292314

2330-
measureChildren.forEach((child) => {
2331-
this.updateColumnDataTypeByAggregator(child);
2332-
})
2315+
measureChildren.forEach((child, index) => {
2316+
const pivotValue = this.values[index];
2317+
PivotUtil.updateColumnTypeByAggregator([child], pivotValue, this.values.length === 1);
2318+
});
23332319

23342320
col.children.reset(measureChildren);
23352321
columns = columns.concat(measureChildren);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class IgxPivotHeaderRowComponent extends IgxGridHeaderRowComponent implem
414414
this.value.aggregate = event.newSelection.value;
415415
const isSingleValue = this.grid.values.length === 1;
416416

417-
PivotUtil.handleCountAggregator(this.grid.columns, this.value, isSingleValue);
417+
PivotUtil.updateColumnTypeByAggregator(this.grid.columns, this.value, isSingleValue);
418418

419419
this.grid.pipeTrigger++;
420420
}

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ export class PivotUtil {
8888
}
8989

9090
public static flattenGroupsHorizontally(data: IPivotGridRecord[],
91-
dimension: IPivotDimension,
92-
expansionStates,
93-
defaultExpand: boolean,
94-
visibleDimensions: IPivotDimension[],
95-
summariesPosition: PivotSummaryPosition,
96-
parent?: IPivotDimension,
97-
parentRec?: IPivotGridRecord) {
91+
dimension: IPivotDimension,
92+
expansionStates,
93+
defaultExpand: boolean,
94+
visibleDimensions: IPivotDimension[],
95+
summariesPosition: PivotSummaryPosition,
96+
parent?: IPivotDimension,
97+
parentRec?: IPivotGridRecord) {
9898
for (let i = 0; i < data.length; i++) {
9999
const rec = data[i];
100100
const field = dimension.memberName;
@@ -316,7 +316,7 @@ export class PivotUtil {
316316
const aggregationKey = groupName ? groupName + pivotKeys.columnDimensionSeparator + key : key;
317317
rec.aggregationValues.set(aggregationKey, aggregationData[key]);
318318
});
319-
} else if (aggregationKeys.length === 1) {
319+
} else if (aggregationKeys.length === 1) {
320320
const aggregationKey = aggregationKeys[0];
321321
rec.aggregationValues.set(groupName || aggregationKey, aggregationData[aggregationKey]);
322322
}
@@ -508,28 +508,24 @@ export class PivotUtil {
508508
}
509509
}
510510

511-
public static handleCountAggregator(columns: ColumnType[], value: IPivotValue, isSingleValue: boolean): void {
511+
public static updateColumnTypeByAggregator(columns: any[], value: IPivotValue, isSingleValue: boolean): void {
512+
const targetColumnType = PivotUtil.getColumnDataTypeForValue(value);
513+
columns.forEach(column => {
514+
if (column.field?.includes(value.member) || isSingleValue) {
515+
column.dataType = targetColumnType;
516+
}
517+
})
518+
}
519+
520+
private static getColumnDataTypeForValue(value: IPivotValue): GridColumnDataType {
512521
const isCountAggregator = value.aggregate.aggregator?.name?.toLowerCase() === 'count' || value.aggregate.aggregatorName?.toLowerCase() === 'count';
513522

514523
if ((value.dataType === GridColumnDataType.Currency || value.dataType === GridColumnDataType.Percent) && isCountAggregator) {
515-
columns.forEach(column => {
516-
console.log(column.field?.includes(value.member))
517-
if (column.field?.includes(value.member) || isSingleValue) {
518-
column.dataType = GridColumnDataType.Number;
519-
}
520-
});
524+
return GridColumnDataType.Number;
521525
} else if (value.dataType === GridColumnDataType.Currency && !isCountAggregator) {
522-
columns.forEach(column => {
523-
if (column.field?.includes(value.member) || isSingleValue) {
524-
column.dataType = GridColumnDataType.Currency;
525-
}
526-
});
526+
return GridColumnDataType.Currency;
527527
} else if (value.dataType === GridColumnDataType.Percent && !isCountAggregator) {
528-
columns.forEach(column => {
529-
if (column.field?.includes(value.member) || isSingleValue) {
530-
column.dataType = GridColumnDataType.Percent;
531-
}
532-
});
528+
return GridColumnDataType.Percent;
533529
}
534530
}
535531
}

0 commit comments

Comments
 (0)