Skip to content

Commit 4421819

Browse files
committed
refactor(pivot-grid): change logic for updating column data type
1 parent 7ad7bc1 commit 4421819

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
@@ -545,48 +545,15 @@ export class IgxPivotDataSelectorComponent {
545545

546546
if (!this.isSelected(event.newSelection.value)) {
547547
this.value.aggregate = event.newSelection.value;
548+
const isSingleValue = this.grid.values.length === 1;
548549

549-
this.handleCountAggregator();
550+
PivotUtil.handleCountAggregator(this.grid.columns, this.value, isSingleValue);
550551

551552
this.grid.pipeTrigger++;
552553
this.grid.cdr.markForCheck();
553554
}
554555
}
555556

556-
private handleCountAggregator() {
557-
const valueMember = this.value.member;
558-
const columns = this.grid.columns;
559-
const isCountAggregator = this.value.aggregate.key.toLowerCase() === 'count';
560-
const isSingleValue = this.grid.values.length === 1;
561-
let shouldRemoveFromSet: boolean = false;
562-
563-
columns.forEach(column => {
564-
const isRelevantColumn = column.field?.includes(valueMember);
565-
const isCurrencyColumn = column.dataType === GridColumnDataType.Currency;
566-
567-
if (isSingleValue) {
568-
if (isCountAggregator && isCurrencyColumn) {
569-
column.dataType = GridColumnDataType.Number;
570-
this.grid.currencyColumnSet.add(valueMember);
571-
} else if (this.grid.currencyColumnSet.has(valueMember)) {
572-
column.dataType = GridColumnDataType.Currency;
573-
}
574-
} else if (isRelevantColumn) {
575-
if (isCountAggregator && isCurrencyColumn) {
576-
column.dataType = GridColumnDataType.Number;
577-
this.grid.currencyColumnSet.add(valueMember);
578-
} else if (this.grid.currencyColumnSet.has(valueMember)) {
579-
column.dataType = GridColumnDataType.Currency;
580-
shouldRemoveFromSet = true;
581-
}
582-
}
583-
});
584-
585-
if (shouldRemoveFromSet) {
586-
this.grid.currencyColumnSet.delete(valueMember);
587-
}
588-
}
589-
590557
/**
591558
* @hidden
592559
* @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
@@ -32,7 +32,7 @@ import {
3232
} from '@angular/core';
3333
import { NgTemplateOutlet, NgClass, NgStyle } from '@angular/common';
3434

35-
import { first, take, takeUntil} from 'rxjs/operators';
35+
import { first, take, takeUntil } from 'rxjs/operators';
3636
import { IgxGridBaseDirective } from '../grid-base.directive';
3737
import { IgxFilteringService } from '../filtering/grid-filtering.service';
3838
import { IgxGridSelectionService } from '../selection/selection.service';
@@ -1666,7 +1666,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
16661666
public autoSizeRowDimension(dimension: IPivotDimension) {
16671667
if (this.getDimensionType(dimension) === PivotDimensionType.Row) {
16681668
const relatedDims: string[] = PivotUtil.flatten([dimension]).map((x: IPivotDimension) => x.memberName);
1669-
const contentCollection = this.getContentCollection(dimension);
1669+
const contentCollection = this.getContentCollection(dimension);
16701670
const content = contentCollection.filter(x => relatedDims.indexOf(x.dimension.memberName) !== -1);
16711671
const headers = content.map(x => x.headerGroups.toArray()).flat().map(x => x.header && x.header.refInstance);
16721672
if (this.pivotUI.showRowHeaders) {
@@ -1940,8 +1940,8 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
19401940
*/
19411941
public getRowDimensionByName(memberName: string) {
19421942
const visibleRows = this.pivotUI.rowLayout === PivotRowLayoutType.Vertical ?
1943-
this.pivotConfiguration.rows :
1944-
PivotUtil.flatten(this.pivotConfiguration.rows);
1943+
this.pivotConfiguration.rows :
1944+
PivotUtil.flatten(this.pivotConfiguration.rows);
19451945
const dimIndex = visibleRows.findIndex((target) => target.memberName === memberName);
19461946
const dim = visibleRows[dimIndex];
19471947
return dim;
@@ -2265,7 +2265,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
22652265
const separator = this.pivotKeys.columnDimensionSeparator;
22662266
const dataArr = fields.map(x => x.split(separator)).sort(x => x.length);
22672267
const hierarchy = new Map<string, any>();
2268-
const columnDimensions = PivotUtil.flatten(this.columnDimensions);
2268+
const columnDimensions = PivotUtil.flatten(this.columnDimensions);
22692269
dataArr.forEach(arr => {
22702270
let currentHierarchy = hierarchy;
22712271
const path = [];
@@ -2285,18 +2285,31 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
22852285
});
22862286
return hierarchy;
22872287
}
2288-
2289-
/** @hidden @internal */
2290-
public currencyColumnSet: Set<string> = new Set();
2288+
private updateColumnDataTypeByAggregator(column: any) {
2289+
this.values.forEach((aggregatorValue) => {
2290+
if ((aggregatorValue.dataType === GridColumnDataType.Currency || aggregatorValue.dataType === GridColumnDataType.Percent) && aggregatorValue.aggregate?.key?.toLowerCase() === 'count') {
2291+
if (column.field?.includes(aggregatorValue.member) || this.values.length === 1) {
2292+
column.dataType = GridColumnDataType.Number;
2293+
}
2294+
} else if (aggregatorValue.dataType === GridColumnDataType.Currency && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2295+
if (column.field?.includes(aggregatorValue.member) || this.values.length === 1) {
2296+
column.dataType = GridColumnDataType.Currency;
2297+
}
2298+
} else if (aggregatorValue.dataType === GridColumnDataType.Percent && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2299+
if (column.field?.includes(aggregatorValue.member) || this.values.length === 1) {
2300+
column.dataType = GridColumnDataType.Percent;
2301+
}
2302+
}
2303+
})
2304+
}
22912305
protected generateColumnHierarchy(fields: Map<string, any>, data, parent = null): IgxColumnComponent[] {
22922306
let columns = [];
22932307
if (fields.size === 0) {
22942308
this.values.forEach((value) => {
22952309
const ref = createComponent(IgxColumnComponent, { environmentInjector: this.envInjector, elementInjector: this.injector });
22962310
let columnDataType = value.dataType || this.resolveDataTypes(data.length ? data[0][value.member] : null);
22972311

2298-
if (value.aggregate?.key?.toLowerCase() === 'count' && columnDataType === GridColumnDataType.Currency) {
2299-
this.currencyColumnSet.add(value.member);
2312+
if (value.aggregate?.key?.toLowerCase() === 'count' && (columnDataType === GridColumnDataType.Currency || columnDataType == GridColumnDataType.Percent)) {
23002313
columnDataType = GridColumnDataType.Number;
23012314
}
23022315

@@ -2319,32 +2332,14 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
23192332
if (shouldGenerate && (value.children == null || value.children.length === 0 || value.children.size === 0)) {
23202333
const col = this.createColumnForDimension(value, data, parent, this.hasMultipleValues);
23212334

2322-
this.values.forEach((aggregatorValue) => {
2323-
if (col.dataType === GridColumnDataType.Currency && aggregatorValue.aggregate?.key?.toLowerCase() === 'count') {
2324-
col.dataType = GridColumnDataType.Number;
2325-
this.currencyColumnSet.add(aggregatorValue.member);
2326-
} else if (this.currencyColumnSet.has(aggregatorValue.member) && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2327-
col.dataType = GridColumnDataType.Currency;
2328-
this.currencyColumnSet.delete(aggregatorValue.member);
2329-
}
2330-
})
2335+
this.updateColumnDataTypeByAggregator(col);
23312336

23322337
columns.push(col);
23332338
if (this.hasMultipleValues) {
23342339
const measureChildren = this.getMeasureChildren(data, col, false, value.dimension.width);
23352340

23362341
measureChildren.forEach((child) => {
2337-
this.values.forEach((aggregatorValue) => {
2338-
if (child.field.includes(aggregatorValue.member)) {
2339-
if (child.dataType === GridColumnDataType.Currency && aggregatorValue.aggregate?.key?.toLowerCase() === 'count') {
2340-
child.dataType = GridColumnDataType.Number;
2341-
this.currencyColumnSet.add(aggregatorValue.member);
2342-
} else if (this.currencyColumnSet.has(aggregatorValue.member) && aggregatorValue.aggregate?.key?.toLowerCase() !== 'count') {
2343-
child.dataType = GridColumnDataType.Currency;
2344-
this.currencyColumnSet.delete(aggregatorValue.member);
2345-
}
2346-
}
2347-
})
2342+
this.updateColumnDataTypeByAggregator(child);
23482343
})
23492344

23502345
col.children.reset(measureChildren);
@@ -2416,20 +2411,20 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
24162411
};
24172412
values.push(value);
24182413
break;
2419-
}
2420-
case "date":
2421-
{
2422-
const dimension: IPivotDimension = new IgxPivotDateDimension(
2414+
}
2415+
case "date":
24232416
{
2424-
memberName: field,
2425-
enabled: isFirstDate,
2426-
dataType: dataType
2417+
const dimension: IPivotDimension = new IgxPivotDateDimension(
2418+
{
2419+
memberName: field,
2420+
enabled: isFirstDate,
2421+
dataType: dataType
2422+
}
2423+
)
2424+
rowDimensions.push(dimension);
2425+
isFirstDate = false;
2426+
break;
24272427
}
2428-
)
2429-
rowDimensions.push(dimension);
2430-
isFirstDate = false;
2431-
break;
2432-
}
24332428
default: {
24342429
const dimension: IPivotDimension = {
24352430
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
@@ -411,47 +411,14 @@ export class IgxPivotHeaderRowComponent extends IgxGridHeaderRowComponent implem
411411

412412
if (!this.isSelected(event.newSelection.value)) {
413413
this.value.aggregate = event.newSelection.value;
414+
const isSingleValue = this.grid.values.length === 1;
414415

415-
this.handleCountAggregator();
416+
PivotUtil.handleCountAggregator(this.grid.columns, this.value, isSingleValue);
416417

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

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