Skip to content

Commit 2093f95

Browse files
authored
Merge pull request #10517 from IgniteUI/mkirova/pivot-column-sort
Pivot grid column sorting.
2 parents 45bb975 + dc0d0a4 commit 2093f95

File tree

10 files changed

+522
-974
lines changed

10 files changed

+522
-974
lines changed

projects/igniteui-angular/src/lib/data-operations/pivot-strategy.ts

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,33 @@ export class PivotRowDimensionsStrategy implements IPivotDimensionStrategy {
2828
}
2929

3030
public process(
31-
collection: any,
32-
rows: IPivotDimension[],
33-
values?: IPivotValue[],
34-
pivotKeys: IPivotKeys =
35-
{ aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'}
36-
): any[] {
37-
let hierarchies;
38-
let data;
39-
const prevRowDims = [];
40-
let prevDim;
41-
for (const row of rows) {
42-
if (!data) {
43-
// build hierarchies - groups and subgroups
44-
hierarchies = PivotUtil.getFieldsHierarchy(collection, [row], PivotDimensionType.Row, pivotKeys);
45-
// generate flat data from the hierarchies
46-
data = PivotUtil.processHierarchy(hierarchies, collection[0] ?? [], pivotKeys, 0, true);
47-
prevRowDims.push(row);
48-
prevDim = row;
49-
} else {
50-
const newData = [...data];
51-
for (let i = 0; i < newData.length; i++) {
52-
const currData = newData[i][prevDim.memberName + '_' + pivotKeys.records];
53-
const hierarchyFields = PivotUtil
54-
.getFieldsHierarchy(currData, [row], PivotDimensionType.Row, pivotKeys);
55-
const siblingData = PivotUtil
56-
.processHierarchy(hierarchyFields, newData[i] ?? [], pivotKeys, 0);
57-
PivotUtil.processSiblingProperties(newData[i], siblingData, pivotKeys);
31+
collection: any,
32+
rows: IPivotDimension[],
33+
values?: IPivotValue[],
34+
pivotKeys: IPivotKeys =
35+
{ aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' }
36+
): any[] {
37+
let hierarchies;
38+
let data;
39+
const prevRowDims = [];
40+
let prevDim;
41+
for (const row of rows) {
42+
if (!data) {
43+
// build hierarchies - groups and subgroups
44+
hierarchies = PivotUtil.getFieldsHierarchy(collection, [row], PivotDimensionType.Row, pivotKeys);
45+
// generate flat data from the hierarchies
46+
data = PivotUtil.processHierarchy(hierarchies, collection[0] ?? [], pivotKeys, 0, true);
47+
prevRowDims.push(row);
48+
prevDim = row;
49+
} else {
50+
const newData = [...data];
51+
for (let i = 0; i < newData.length; i++) {
52+
const currData = newData[i][prevDim.memberName + '_' + pivotKeys.records];
53+
const hierarchyFields = PivotUtil
54+
.getFieldsHierarchy(currData, [row], PivotDimensionType.Row, pivotKeys);
55+
const siblingData = PivotUtil
56+
.processHierarchy(hierarchyFields, newData[i] ?? [], pivotKeys, 0);
57+
PivotUtil.processSiblingProperties(newData[i], siblingData, pivotKeys);
5858

5959
PivotUtil.processSubGroups(row, prevRowDims.slice(0), siblingData, pivotKeys);
6060
if (siblingData.length > 1) {
@@ -81,43 +81,57 @@ export class PivotColumnDimensionsStrategy implements IPivotDimensionStrategy {
8181
}
8282

8383
public process(
84-
collection: any[],
85-
columns: IPivotDimension[],
86-
values: IPivotValue[],
87-
pivotKeys: IPivotKeys = {aggregations: 'aggregations', records: 'records', children: 'children', level: 'level'}
88-
): any[] {
89-
const result = [];
90-
collection.forEach(hierarchy => {
91-
// apply aggregations based on the created groups and generate column fields based on the hierarchies
92-
this.groupColumns(hierarchy, columns, values, pivotKeys);
93-
if (hierarchy[pivotKeys.children]) {
94-
let flatCols = {};
95-
PivotUtil.flattenColumnHierarchy(hierarchy[pivotKeys.children], values, pivotKeys).forEach(o => {
96-
delete o[pivotKeys.records];
97-
flatCols = {...flatCols, ...o};
98-
});
99-
delete hierarchy[pivotKeys.children]; /* or we can keep it
100-
and use when creating the columns in pivot grid instead of recreating it */
101-
const keys = Object.keys(hierarchy);
102-
//remove all record keys from final data since we don't need them anymore.
103-
keys.forEach(k => {
104-
if (k.indexOf(pivotKeys.records) !== -1 || k === pivotKeys.level) {
105-
delete hierarchy[k];
84+
collection: any[],
85+
columns: IPivotDimension[],
86+
values: IPivotValue[],
87+
pivotKeys: IPivotKeys = { aggregations: 'aggregations', records: 'records', children: 'children', level: 'level' }
88+
): any[] {
89+
const res = this.processHierarchy(collection, columns, values, pivotKeys);
90+
return res;
91+
}
92+
93+
private processHierarchy(collection, columns: IPivotDimension[], values, pivotKeys) {
94+
const result = [];
95+
collection.forEach(hierarchy => {
96+
// apply aggregations based on the created groups and generate column fields based on the hierarchies
97+
this.groupColumns(hierarchy, columns, values, pivotKeys);
98+
if (hierarchy[pivotKeys.children]) {
99+
let flatCols = {};
100+
PivotUtil.flattenColumnHierarchy(hierarchy[pivotKeys.children], values, pivotKeys).forEach(o => {
101+
delete o[pivotKeys.records];
102+
flatCols = { ...flatCols, ...o };
103+
});
104+
delete hierarchy[pivotKeys.children]; /* or we can keep it
105+
and use when creating the columns in pivot grid instead of recreating it */
106+
const keys = Object.keys(hierarchy);
107+
//remove all record keys from final data since we don't need them anymore.
108+
hierarchy.processed = true;
109+
keys.forEach(k => {
110+
if (k.indexOf(pivotKeys.records) !== -1) {
111+
if (hierarchy[k] && hierarchy[k].length > 0 && k !== pivotKeys.records) {
112+
const unprocessed = hierarchy[k].filter(r => !r.processed);
113+
this.processHierarchy(unprocessed, columns, values, pivotKeys);
106114
}
107-
});
108-
if (this.isLeaf(hierarchy, pivotKeys)) {
109-
delete hierarchy[pivotKeys.records]; /* remove the helper records of the actual records so that
110-
expand indicators can be rendered properly */
115+
//delete hierarchy[k];
111116
}
112-
for (const property in flatCols) {
113-
if (flatCols.hasOwnProperty(property)) {
114-
hierarchy[property] = flatCols[property];
115-
}
117+
if (k === pivotKeys.level) {
118+
delete hierarchy[k];
119+
}
120+
});
121+
delete hierarchy.processed;
122+
if (this.isLeaf(hierarchy, pivotKeys)) {
123+
delete hierarchy[pivotKeys.records]; /* remove the helper records of the actual records so that
124+
expand indicators can be rendered properly */
125+
}
126+
for (const property in flatCols) {
127+
if (flatCols.hasOwnProperty(property)) {
128+
hierarchy[property] = flatCols[property];
116129
}
117-
result.push(hierarchy);
118130
}
119-
});
120-
return result;
131+
result.push(hierarchy);
132+
}
133+
});
134+
return result;
121135
}
122136

123137
private groupColumns(hierarchy, columns, values, pivotKeys) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
<ng-template igxGridFor let-rowData [igxGridForOf]="data
3333
| pivotGridFilter:pivotConfiguration:filterStrategy:advancedFilteringExpressionsTree:filteringPipeTrigger:pipeTrigger
3434
| pivotGridSort:pivotConfiguration:sortStrategy:id:pipeTrigger
35-
| pivotGridRow:pivotConfiguration:expansionStates:pipeTrigger
36-
| pivotGridRowExpansion:pivotConfiguration:expansionStates:pipeTrigger
37-
| pivotGridColumn:pivotConfiguration:expansionStates:pipeTrigger"
35+
| pivotGridRow:pivotConfiguration:expansionStates:pipeTrigger:sortingExpressions
36+
| pivotGridColumn:pivotConfiguration:expansionStates:pipeTrigger:sortingExpressions
37+
| pivotGridColumnSort:sortingExpressions:sortStrategy:pipeTrigger
38+
| pivotGridRowExpansion:pivotConfiguration:expansionStates:pipeTrigger"
3839
let-rowIndex="index" [igxForScrollOrientation]="'vertical'" [igxForScrollContainer]='verticalScroll'
3940
[igxForContainerSize]='calcHeight'
4041
[igxForItemSize]="hasColumnLayouts ? rowHeight * multiRowLayoutRowSize + 1 : renderedRowHeight"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10111011
ref.instance.parent = parent;
10121012
ref.instance.dataType = this.pivotConfiguration.values[0]?.dataType || this.resolveDataTypes(data[0][key]);
10131013
ref.instance.formatter = this.pivotConfiguration.values[0]?.formatter;
1014+
ref.instance.sortable = true;
10141015
ref.changeDetectorRef.detectChanges();
10151016
columns.push(ref.instance);
10161017
if (this.hasMultipleValues) {
@@ -1023,6 +1024,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10231024
const ref = factoryColumnGroup.create(this.viewRef.injector);
10241025
ref.instance.parent = parent;
10251026
ref.instance.field = key;
1027+
ref.instance.sortable = true;
10261028
ref.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
10271029
if (value.expandable) {
10281030
ref.instance.headerTemplate = this.headerTemplate;
@@ -1032,6 +1034,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10321034
refSibling.instance.header = parent != null ? key.split(parent.header + '-')[1] : key;
10331035
refSibling.instance.field = key;
10341036
refSibling.instance.parent = parent;
1037+
ref.instance.sortable = true;
10351038
refSibling.instance.hidden = true;
10361039
refSibling.instance.dataType = this.pivotConfiguration.values[0]?.dataType || this.resolveDataTypes(data[0][key]);
10371040
refSibling.instance.formatter = this.pivotConfiguration.values[0]?.formatter;
@@ -1065,6 +1068,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
10651068
ref.instance.field = parent.field + '-' + val.member;
10661069
ref.instance.parent = parent;
10671070
ref.instance.hidden = hidden;
1071+
ref.instance.sortable = true;
10681072
ref.instance.dataType = val.dataType || this.resolveDataTypes(data[0][val.member]);
10691073
ref.instance.formatter = val.formatter;
10701074
ref.changeDetectorRef.detectChanges();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IgxGridModule } from '../grid/grid.module';
33
import { IgxPivotGridComponent } from './pivot-grid.component';
44
import { IgxPivotRowComponent } from './pivot-row.component';
55
import { IgxPivotRowPipe, IgxPivotColumnPipe, IgxPivotGridFilterPipe,
6-
IgxPivotRowExpansionPipe, IgxPivotGridSortingPipe } from './pivot-grid.pipes';
6+
IgxPivotRowExpansionPipe, IgxPivotGridSortingPipe, IgxPivotGridColumnSortingPipe } from './pivot-grid.pipes';
77
import { IgxGridComponent } from '../grid/grid.component';
88
import { IgxPivotHeaderRowComponent } from './pivot-header-row.component';
99
import { IgxPivotRowDimensionContentComponent } from './pivot-row-dimension-content.component';
@@ -21,7 +21,8 @@ import { IgxPivotRowDimensionContentComponent } from './pivot-row-dimension-cont
2121
IgxPivotRowExpansionPipe,
2222
IgxPivotColumnPipe,
2323
IgxPivotGridFilterPipe,
24-
IgxPivotGridSortingPipe
24+
IgxPivotGridSortingPipe,
25+
IgxPivotGridColumnSortingPipe
2526
],
2627
exports: [
2728
IgxGridModule,
@@ -33,7 +34,8 @@ import { IgxPivotRowDimensionContentComponent } from './pivot-row-dimension-cont
3334
IgxPivotRowPipe,
3435
IgxPivotColumnPipe,
3536
IgxPivotGridFilterPipe,
36-
IgxPivotGridSortingPipe
37+
IgxPivotGridSortingPipe,
38+
IgxPivotGridColumnSortingPipe
3739
],
3840
imports: [
3941
IgxGridModule,

0 commit comments

Comments
 (0)