Skip to content

Commit 038a431

Browse files
committed
refactor(pivot-grid): change logic for updating column data type
1 parent 4c5c837 commit 038a431

File tree

5 files changed

+65
-115
lines changed

5 files changed

+65
-115
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,6 @@ export interface PivotGridType extends GridType {
13381338
excelStyleFilterMinHeight: string;
13391339
valueChipTemplate: TemplateRef<any>;
13401340
rowDimensionHeaderTemplate: TemplateRef<IgxColumnTemplateContext>;
1341-
/** @hidden @internal */
1342-
currencyColumnSet: Set<string>
13431341
}
13441342

13451343
export interface GridSVGIcon {

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -521,48 +521,15 @@ export class IgxPivotDataSelectorComponent {
521521

522522
if (!this.isSelected(event.newSelection.value)) {
523523
this.value.aggregate = event.newSelection.value;
524+
const isSingleValue = this.grid.values.length === 1;
524525

525-
this.handleCountAggregator();
526+
PivotUtil.handleCountAggregator(this.grid.columns, this.value, isSingleValue);
526527

527528
this.grid.pipeTrigger++;
528529
this.grid.cdr.markForCheck();
529530
}
530531
}
531532

532-
private handleCountAggregator() {
533-
const valueMember = this.value.member;
534-
const columns = this.grid.columns;
535-
const isCountAggregator = this.value.aggregate.key.toLowerCase() === 'count';
536-
const isSingleValue = this.grid.values.length === 1;
537-
let shouldRemoveFromSet: boolean = false;
538-
539-
columns.forEach(column => {
540-
const isRelevantColumn = column.field?.includes(valueMember);
541-
const isCurrencyColumn = column.dataType === GridColumnDataType.Currency;
542-
543-
if (isSingleValue) {
544-
if (isCountAggregator && isCurrencyColumn) {
545-
column.dataType = GridColumnDataType.Number;
546-
this.grid.currencyColumnSet.add(valueMember);
547-
} else if (this.grid.currencyColumnSet.has(valueMember)) {
548-
column.dataType = GridColumnDataType.Currency;
549-
}
550-
} else if (isRelevantColumn) {
551-
if (isCountAggregator && isCurrencyColumn) {
552-
column.dataType = GridColumnDataType.Number;
553-
this.grid.currencyColumnSet.add(valueMember);
554-
} else if (this.grid.currencyColumnSet.has(valueMember)) {
555-
column.dataType = GridColumnDataType.Currency;
556-
shouldRemoveFromSet = true;
557-
}
558-
}
559-
});
560-
561-
if (shouldRemoveFromSet) {
562-
this.grid.currencyColumnSet.delete(valueMember);
563-
}
564-
}
565-
566533
/**
567534
* @hidden
568535
* @internal

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

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from '@angular/core';
3232
import { DOCUMENT, NgTemplateOutlet, NgClass, NgStyle } from '@angular/common';
3333

34-
import { first, take, takeUntil} from 'rxjs/operators';
34+
import { first, take, takeUntil } from 'rxjs/operators';
3535
import { IgxGridBaseDirective } from '../grid-base.directive';
3636
import { IgxFilteringService } from '../filtering/grid-filtering.service';
3737
import { IgxGridSelectionService } from '../selection/selection.service';
@@ -1655,7 +1655,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
16551655
public autoSizeRowDimension(dimension: IPivotDimension) {
16561656
if (this.getDimensionType(dimension) === PivotDimensionType.Row) {
16571657
const relatedDims: string[] = PivotUtil.flatten([dimension]).map((x: IPivotDimension) => x.memberName);
1658-
const contentCollection = this.getContentCollection(dimension);
1658+
const contentCollection = this.getContentCollection(dimension);
16591659
const content = contentCollection.filter(x => relatedDims.indexOf(x.dimension.memberName) !== -1);
16601660
const headers = content.map(x => x.headerGroups.toArray()).flat().map(x => x.header && x.header.refInstance);
16611661
if (this.pivotUI.showRowHeaders) {
@@ -1929,8 +1929,8 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
19291929
*/
19301930
public getRowDimensionByName(memberName: string) {
19311931
const visibleRows = this.pivotUI.rowLayout === PivotRowLayoutType.Vertical ?
1932-
this.pivotConfiguration.rows :
1933-
PivotUtil.flatten(this.pivotConfiguration.rows);
1932+
this.pivotConfiguration.rows :
1933+
PivotUtil.flatten(this.pivotConfiguration.rows);
19341934
const dimIndex = visibleRows.findIndex((target) => target.memberName === memberName);
19351935
const dim = visibleRows[dimIndex];
19361936
return dim;
@@ -2254,7 +2254,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
22542254
const separator = this.pivotKeys.columnDimensionSeparator;
22552255
const dataArr = fields.map(x => x.split(separator)).sort(x => x.length);
22562256
const hierarchy = new Map<string, any>();
2257-
const columnDimensions = PivotUtil.flatten(this.columnDimensions);
2257+
const columnDimensions = PivotUtil.flatten(this.columnDimensions);
22582258
dataArr.forEach(arr => {
22592259
let currentHierarchy = hierarchy;
22602260
const path = [];
@@ -2274,18 +2274,31 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
22742274
});
22752275
return hierarchy;
22762276
}
2277-
2278-
/** @hidden @internal */
2279-
public currencyColumnSet: Set<string> = new Set();
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+
}
22802294
protected generateColumnHierarchy(fields: Map<string, any>, data, parent = null): IgxColumnComponent[] {
22812295
let columns = [];
22822296
if (fields.size === 0) {
22832297
this.values.forEach((value) => {
22842298
const ref = createComponent(IgxColumnComponent, { environmentInjector: this.envInjector, elementInjector: this.injector });
22852299
let columnDataType = value.dataType || this.resolveDataTypes(data.length ? data[0][value.member] : null);
22862300

2287-
if (value.aggregate?.key?.toLowerCase() === 'count' && columnDataType === GridColumnDataType.Currency) {
2288-
this.currencyColumnSet.add(value.member);
2301+
if (value.aggregate?.key?.toLowerCase() === 'count' && (columnDataType === GridColumnDataType.Currency || columnDataType == GridColumnDataType.Percent)) {
22892302
columnDataType = GridColumnDataType.Number;
22902303
}
22912304

@@ -2308,32 +2321,14 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
23082321
if (shouldGenerate && (value.children == null || value.children.length === 0 || value.children.size === 0)) {
23092322
const col = this.createColumnForDimension(value, data, parent, this.hasMultipleValues);
23102323

2311-
this.values.forEach((aggregatorValue) => {
2312-
if (col.dataType === GridColumnDataType.Currency && aggregatorValue.aggregate?.key?.toLowerCase() === 'count') {
2313-
col.dataType = GridColumnDataType.Number;
2314-
this.currencyColumnSet.add(aggregatorValue.member);
2315-
} else if (this.currencyColumnSet.has(aggregatorValue.member) && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2316-
col.dataType = GridColumnDataType.Currency;
2317-
this.currencyColumnSet.delete(aggregatorValue.member);
2318-
}
2319-
})
2324+
this.updateColumnDataTypeByAggregator(col);
23202325

23212326
columns.push(col);
23222327
if (this.hasMultipleValues) {
23232328
const measureChildren = this.getMeasureChildren(data, col, false, value.dimension.width);
23242329

23252330
measureChildren.forEach((child) => {
2326-
this.values.forEach((aggregatorValue) => {
2327-
if (child.field.includes(aggregatorValue.member)) {
2328-
if (child.dataType === GridColumnDataType.Currency && aggregatorValue.aggregate?.key?.toLowerCase() === 'count') {
2329-
child.dataType = GridColumnDataType.Number;
2330-
this.currencyColumnSet.add(aggregatorValue.member);
2331-
} else if (this.currencyColumnSet.has(aggregatorValue.member) && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2332-
child.dataType = GridColumnDataType.Currency;
2333-
this.currencyColumnSet.delete(aggregatorValue.member);
2334-
}
2335-
}
2336-
})
2331+
this.updateColumnDataTypeByAggregator(child);
23372332
})
23382333

23392334
col.children.reset(measureChildren);
@@ -2405,20 +2400,20 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
24052400
};
24062401
values.push(value);
24072402
break;
2408-
}
2409-
case "date":
2410-
{
2411-
const dimension: IPivotDimension = new IgxPivotDateDimension(
2403+
}
2404+
case "date":
24122405
{
2413-
memberName: field,
2414-
enabled: isFirstDate,
2415-
dataType: dataType
2406+
const dimension: IPivotDimension = new IgxPivotDateDimension(
2407+
{
2408+
memberName: field,
2409+
enabled: isFirstDate,
2410+
dataType: dataType
2411+
}
2412+
)
2413+
rowDimensions.push(dimension);
2414+
isFirstDate = false;
2415+
break;
24162416
}
2417-
)
2418-
rowDimensions.push(dimension);
2419-
isFirstDate = false;
2420-
break;
2421-
}
24222417
default: {
24232418
const dimension: IPivotDimension = {
24242419
memberName: field,

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -412,47 +412,14 @@ export class IgxPivotHeaderRowComponent extends IgxGridHeaderRowComponent implem
412412

413413
if (!this.isSelected(event.newSelection.value)) {
414414
this.value.aggregate = event.newSelection.value;
415+
const isSingleValue = this.grid.values.length === 1;
415416

416-
this.handleCountAggregator();
417+
PivotUtil.handleCountAggregator(this.grid.columns, this.value, isSingleValue);
417418

418419
this.grid.pipeTrigger++;
419420
}
420421
}
421422

422-
private handleCountAggregator() {
423-
const valueMember = this.value.member;
424-
const columns = this.grid.columns;
425-
const isCountAggregator = this.value.aggregate.key.toLowerCase() === 'count';
426-
const isSingleValue = this.grid.values.length === 1;
427-
let shouldRemoveFromSet: boolean = false;
428-
429-
columns.forEach(column => {
430-
const isRelevantColumn = column.field?.includes(valueMember);
431-
const isCurrencyColumn = column.dataType === GridColumnDataType.Currency;
432-
433-
if (isSingleValue) {
434-
if (isCountAggregator && isCurrencyColumn) {
435-
column.dataType = GridColumnDataType.Number;
436-
this.grid.currencyColumnSet.add(valueMember);
437-
} else if (this.grid.currencyColumnSet.has(valueMember)) {
438-
column.dataType = GridColumnDataType.Currency;
439-
}
440-
} else if (isRelevantColumn) {
441-
if (isCountAggregator && isCurrencyColumn) {
442-
column.dataType = GridColumnDataType.Number;
443-
this.grid.currencyColumnSet.add(valueMember);
444-
} else if (this.grid.currencyColumnSet.has(valueMember)) {
445-
column.dataType = GridColumnDataType.Currency;
446-
shouldRemoveFromSet = true;
447-
}
448-
}
449-
});
450-
451-
if (shouldRemoveFromSet) {
452-
this.grid.currencyColumnSet.delete(valueMember);
453-
}
454-
}
455-
456423
/**
457424
* @hidden
458425
* @internal

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DataUtil, GridColumnDataType } from '../../data-operations/data-util';
33
import { FilteringLogic } from '../../data-operations/filtering-expression.interface';
44
import { FilteringExpressionsTree } from '../../data-operations/filtering-expressions-tree';
55
import { ISortingExpression } from '../../data-operations/sorting-strategy';
6-
import { PivotGridType } from '../common/grid.interface';
6+
import { ColumnType, PivotGridType } from '../common/grid.interface';
77
import { IGridSortingStrategy, IgxSorting } from '../common/strategy';
88
import { IgxPivotAggregate, IgxPivotDateAggregate, IgxPivotNumericAggregate, IgxPivotTimeAggregate } from './pivot-grid-aggregate';
99
import { IPivotAggregator, IPivotConfiguration, IPivotDimension, IPivotGridRecord, IPivotKeys, IPivotValue, PivotDimensionType, PivotSummaryPosition } from './pivot-grid.interface';
@@ -508,5 +508,28 @@ export class PivotUtil {
508508
}
509509
}
510510

511+
public static handleCountAggregator(columns: ColumnType[], value: IPivotValue, isSingleValue: boolean): void {
512+
const isCountAggregator = value.aggregate.aggregator?.name?.toLowerCase() === 'count' || value.aggregate.aggregatorName?.toLowerCase() === 'count';
511513

514+
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+
});
521+
} 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+
});
527+
} 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+
});
533+
}
534+
}
512535
}

0 commit comments

Comments
 (0)