Skip to content

Commit 748a398

Browse files
Svetloslav15kdinevhanastasovChronosSF
authored
feat(grid): sort groups by values count (#12734)
* feat(grid): add sorting by group count * chore(grid): remove unused variable * fix(sorting): rename group sorting strategy * feat(grid): update changelog with sorting strategy * fix(grid): issue about group sorting * fix(grid): issue about group sorting --------- Co-authored-by: Konstantin Dinev <[email protected]> Co-authored-by: Hristo <[email protected]> Co-authored-by: Stamen Stoychev <[email protected]>
1 parent fba2569 commit 748a398

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes for each version of this project will be documented in this
55
## 15.1.0
66

77
### New Features
8+
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`:
9+
- `GroupMemberCountSortingStrategy` is added, which can be used to sort the grid by number of items in each group in ASC or DESC order, if grouping is applied.
810
- `IgxColumnComponent`
911
- Added `currRec` and `groupRec` parameters to the `groupingComparer` function that give access to the all properties of the compared records.
1012

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,41 @@ export class DefaultSortingStrategy implements ISortingStrategy {
8787
protected arraySort(data: any[], compareFn?: (arg0: any, arg1: any) => number): any[] {
8888
return data.sort(compareFn);
8989
}
90+
}
91+
92+
export class GroupMemberCountSortingStrategy implements ISortingStrategy {
93+
protected static _instance: GroupMemberCountSortingStrategy = null;
94+
95+
protected constructor() { }
96+
97+
public static instance(): GroupMemberCountSortingStrategy {
98+
return this._instance || (this._instance = new this());
99+
}
100+
101+
public sort(data: any[], fieldName: string, dir: SortingDirection) {
102+
const groupedArray = this.groupBy(data, fieldName);
103+
const reverse = (dir === SortingDirection.Desc ? -1 : 1);
104+
105+
const cmpFunc = (a, b) => {
106+
return this.compareObjects(a, b, groupedArray, fieldName, reverse);
107+
};
108+
109+
return data
110+
.sort((a, b) => a[fieldName].localeCompare(b[fieldName]))
111+
.sort(cmpFunc);
112+
}
113+
114+
public groupBy(data, key) {
115+
return data.reduce((acc, curr) => {
116+
(acc[curr[key]] = acc[curr[key]] || []).push(curr);
117+
return acc;
118+
}, {})
119+
}
120+
121+
protected compareObjects(obj1: any, obj2: any, data: any[], fieldName: string, reverse: number) {
122+
let firstItemValuesLength = data[obj1[fieldName]].length;
123+
let secondItemValuesLength = data[obj2[fieldName]].length;
124+
125+
return reverse * (firstItemValuesLength - secondItemValuesLength);
126+
}
90127
}

src/app/grid-groupby/grid-groupby.sample.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
</igx-grid>
5151
<br />
52-
52+
<button igxButton="raised" (click)="sortByGroup()">Sort by group ASC/DESC</button>
5353
<button igxButton="raised" (click)="groupBy('CompanyName')">Group/Ungroup 'CompanyName'</button>
5454
<button igxButton="raised" (click)="groupBy('ContactTitle')">Group/Ungroup 'ContactTitle'</button>
5555
<button igxButton="raised" (click)="groupBy('City')">Group/Ungroup 'City'</button>

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Component, ViewChild, OnInit, Inject } from '@angular/core';
33

44
import {
55
IgxGridComponent, SortingDirection, ISortingExpression,
6-
DefaultSortingStrategy, DisplayDensity, IDisplayDensityOptions, DisplayDensityToken, GridSummaryPosition, GridSummaryCalculationMode, IRowSelectionEventArgs, ISortingOptions
6+
DefaultSortingStrategy, DisplayDensity, IDisplayDensityOptions, DisplayDensityToken, GridSummaryPosition, GridSummaryCalculationMode, IRowSelectionEventArgs, ISortingOptions, GroupMemberCountSortingStrategy
77
} from 'igniteui-angular';
88

99
@Component({
@@ -22,12 +22,12 @@ export class GridGroupBySampleComponent implements OnInit {
2222
public expState = [];
2323
public columns: Array<any>;
2424
public groupingExpressions: Array<ISortingExpression>;
25-
public perfGrpExpr = [ { fieldName: 'FIELD', dir: SortingDirection.Asc } ];
25+
public perfGrpExpr = [{ fieldName: 'FIELD', dir: SortingDirection.Asc }];
2626
public summaryMode: GridSummaryCalculationMode = GridSummaryCalculationMode.rootLevelOnly;
2727
public summaryModes = [];
2828
public selectionModes: any[];
2929
public position: GridSummaryPosition = GridSummaryPosition.top;
30-
public sortingOp: ISortingOptions = {mode: 'multiple'};
30+
public sortingOp: ISortingOptions = { mode: 'multiple' };
3131

3232
private _density: DisplayDensity = DisplayDensity.cosy;
3333

@@ -40,7 +40,7 @@ export class GridGroupBySampleComponent implements OnInit {
4040
this.data2.push(...Array(10).fill({ STATUS: 'C', FIELD: 'some text' }));
4141
this.data2.push(...Array(10).fill({ STATUS: 'D', FIELD: 'some text' }));
4242
}
43-
this.data2 = this.data2.map((rec, index) => ({...rec, ID: index}));
43+
this.data2 = this.data2.map((rec, index) => ({ ...rec, ID: index }));
4444
this.columns = [
4545
{ dataType: 'string', field: 'ID', width: 100, hidden: true },
4646
{ dataType: 'string', field: 'CompanyName', width: 300, groupable: true },
@@ -112,6 +112,15 @@ export class GridGroupBySampleComponent implements OnInit {
112112
this.grid1.groupBy({ fieldName: name, dir: SortingDirection.Asc, ignoreCase: false, strategy: DefaultSortingStrategy.instance() });
113113
}
114114

115+
public sortByGroup() {
116+
const expressions = this.grid1.groupingExpressions;
117+
if (expressions.length) {
118+
const fieldName = expressions[0].fieldName;
119+
const dir = expressions[0].dir === SortingDirection.Asc ? SortingDirection.Desc : SortingDirection.Asc;
120+
this.grid1.groupBy({ fieldName, dir, ignoreCase: false, strategy: GroupMemberCountSortingStrategy.instance() });
121+
}
122+
}
123+
115124
public toggleGroupedVisibility(event) {
116125
this.grid1.hideGroupedColumns = !event.checked;
117126
}
@@ -179,4 +188,4 @@ export class GridGroupBySampleComponent implements OnInit {
179188
public rowSelectionChanged(e: IRowSelectionEventArgs) {
180189
console.log(e);
181190
}
182-
}
191+
}

0 commit comments

Comments
 (0)