Skip to content

Commit 127433f

Browse files
viktorkombovSvetloslav15kdinev
authored
fix(grid): Allow using custom sorting strategy when sorting mode is multiple (#12682)
* fix(grid): Allow using custom sorting strategy when sorting mode is multiple * chore(*): add an optional chaining operator to prevent throwing an error * chore(*): update changelog for adding two new params to groupingComparer --------- Co-authored-by: Svetloslav Novoselski <[email protected]> Co-authored-by: Konstantin Dinev <[email protected]>
1 parent e492fab commit 127433f

File tree

6 files changed

+79
-15
lines changed

6 files changed

+79
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
All notable changes for each version of this project will be documented in this file.
44

55
## 15.1.0
6+
7+
### New Features
8+
- `IgxColumnComponent`
9+
- Added `currRec` and `groupRec` parameters to the `groupingComparer` function that give access to the all properties of the compared records.
10+
611
### General
712
- `IgxPivotGrid`
813
- The `IgxPivotDateDimension` properties `inBaseDimension` and `inOption` have been deprecated and renamed to `baseDimension` and `options` respectively.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ISortingExpression } from './sorting-strategy';
22

33
export interface IGroupingExpression extends ISortingExpression {
4-
groupingComparer?: (a: any, b: any) => number;
4+
groupingComparer?: (a: any, b: any, currRec?: any, groupRec?: any) => number;
55
}

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,19 +1110,19 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
11101110
* @memberof IgxColumnComponent
11111111
*/
11121112
@Input()
1113-
public get groupingComparer(): (a: any, b: any) => number {
1113+
public get groupingComparer(): (a: any, b: any, currRec?: any, groupRec?: any) => number {
11141114
return this._groupingComparer;
11151115
}
11161116
/**
11171117
* Sets a custom function to compare values for grouping.
11181118
* Subsequent values in the sorted data that the function returns 0 for are grouped.
11191119
* ```typescript
1120-
* this.column.groupingComparer = (a: any, b: any) => { return a === b ? 0 : -1; }
1120+
* this.column.groupingComparer = (a: any, b: any, currRec?: any, groupRec?: any) => { return a === b ? 0 : -1; }
11211121
* ```
11221122
*
11231123
* @memberof IgxColumnComponent
11241124
*/
1125-
public set groupingComparer(funcRef: (a: any, b: any) => number) {
1125+
public set groupingComparer(funcRef: (a: any, b: any, currRec?: any, groupRec?: any) => number) {
11261126
this._groupingComparer = funcRef;
11271127
}
11281128
/**
@@ -1698,7 +1698,7 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
16981698
/**
16991699
* @hidden
17001700
*/
1701-
protected _groupingComparer: (a: any, b: any) => number;
1701+
protected _groupingComparer: (a: any, b: any, currRec?: any, groupRec?: any) => number;
17021702
/**
17031703
* @hidden
17041704
*/

projects/igniteui-angular/src/lib/grids/common/strategy.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,27 @@ export class IgxSorting implements IGridSortingStrategy {
110110
expression: IGroupingExpression,
111111
isDate: boolean = false,
112112
isTime: boolean = false,
113-
isString: boolean
113+
isString: boolean,
114+
groupingComparer?: (a: any, b: any, currRec: any, groupRec: any) => number
114115
): T[] {
115116
const res = [];
116117
const key = expression.fieldName;
117118
const len = data.length;
118-
let groupval = this.getFieldValue(data[index], key, isDate, isTime);
119-
res.push(data[index]);
119+
const groupRecord = data[index];
120+
let groupval = this.getFieldValue(groupRecord, key, isDate, isTime);
121+
res.push(groupRecord);
120122
index++;
121-
const comparer = expression.groupingComparer || DefaultSortingStrategy.instance().compareValues;
123+
const comparer = expression.groupingComparer || groupingComparer || DefaultSortingStrategy.instance().compareValues;
122124
for (let i = index; i < len; i++) {
123-
let fieldValue = this.getFieldValue(data[i], key, isDate, isTime);
125+
const currRec = data[i];
126+
let fieldValue = this.getFieldValue(currRec, key, isDate, isTime);
124127
if (expression.ignoreCase && isString) {
125128
// when column's dataType is string but the value is number
126129
fieldValue = fieldValue?.toString().toLowerCase();
127130
groupval = groupval?.toString().toLowerCase();
128131
}
129-
if (comparer(fieldValue, groupval) === 0) {
130-
res.push(data[i]);
132+
if (comparer(fieldValue, groupval, currRec, groupRecord) === 0) {
133+
res.push(currRec);
131134
} else {
132135
break;
133136
}
@@ -166,7 +169,7 @@ export class IgxSorting implements IGridSortingStrategy {
166169
}
167170
// in case of multiple sorting
168171
for (i = 0; i < dataLen; i++) {
169-
gbData = this.groupedRecordsByExpression(data, i, expr, isDate, isTime, isString);
172+
gbData = this.groupedRecordsByExpression(data, i, expr, isDate, isTime, isString, column?.groupingComparer);
170173
gbDataLen = gbData.length;
171174
if (gbDataLen > 1) {
172175
gbData = this.sortDataRecursive(gbData, expressions, expressionIndex + 1, grid);

projects/igniteui-angular/src/lib/grids/grid/grid.sorting.spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { DefaultSortingStrategy, SortingDirection } from '../../data-operations/
55
import { configureTestSuite } from '../../test-utils/configure-suite';
66
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
77
import { GridFunctions } from '../../test-utils/grid-functions.spec';
8-
import { GridDeclaredColumnsComponent, SortByParityComponent, GridWithPrimaryKeyComponent } from '../../test-utils/grid-samples.spec';
8+
import { GridDeclaredColumnsComponent, SortByParityComponent, GridWithPrimaryKeyComponent, SortByAnotherColumnComponent } from '../../test-utils/grid-samples.spec';
99
import { UIInteractions } from '../../test-utils/ui-interactions.spec';
1010
import { SampleTestData } from '../../test-utils/sample-test-data.spec';
1111
import { CellType } from '../common/grid.interface';
@@ -21,7 +21,8 @@ describe('IgxGrid - Grid Sorting #grid', () => {
2121
declarations: [
2222
GridDeclaredColumnsComponent,
2323
SortByParityComponent,
24-
GridWithPrimaryKeyComponent
24+
GridWithPrimaryKeyComponent,
25+
SortByAnotherColumnComponent
2526
],
2627
imports: [NoopAnimationsModule, IgxGridModule]
2728
});
@@ -356,6 +357,37 @@ describe('IgxGrid - Grid Sorting #grid', () => {
356357
expect(isFirstHalfOdd).toEqual(true);
357358
expect(isSecondHalfEven).toEqual(true);
358359
});
360+
361+
it(`Should allow sorting using a custom Sorting Strategy in multiple mode`, () => {
362+
fixture = TestBed.createComponent(SortByAnotherColumnComponent);
363+
grid = fixture.componentInstance.grid;
364+
fixture.detectChanges();
365+
366+
grid.primaryKey = 'ID';
367+
const column = grid.getColumnByName('ID');
368+
fixture.detectChanges();
369+
370+
column.groupingComparer = (a: any, b: any, currRec: any, groupRec: any) => {
371+
return currRec.Name === groupRec.Name ? 0 : -1;
372+
}
373+
374+
fixture.detectChanges();
375+
grid.sortingExpressions = [
376+
{
377+
dir: SortingDirection.Asc,
378+
fieldName: 'ID',
379+
strategy: new SortByAnotherColumnComponent,
380+
},
381+
{
382+
dir: SortingDirection.Asc,
383+
fieldName: 'LastName',
384+
strategy: DefaultSortingStrategy.instance(),
385+
},
386+
];
387+
fixture.detectChanges();
388+
expect(grid.getCellByKey(6, 'LastName').row.index).toBeGreaterThan(grid.getCellByKey(7, 'LastName').row.index);
389+
expect(grid.getCellByKey(4, 'LastName').row.index).toBeGreaterThan(grid.getCellByKey(5, 'LastName').row.index);
390+
});
359391
});
360392

361393
describe('UI tests', () => {

projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,30 @@ export class SortByParityComponent extends GridDeclaredColumnsComponent implemen
23662366
}
23672367
}
23682368

2369+
@Component({
2370+
template: GridTemplateStrings.declareGrid(
2371+
'',
2372+
'',
2373+
ColumnDefinitions.idFirstLastNameSortable,
2374+
'',
2375+
'',
2376+
'')
2377+
})
2378+
export class SortByAnotherColumnComponent extends GridDeclaredColumnsComponent implements ISortingStrategy {
2379+
2380+
public sort(data: any[]) {
2381+
const key = 'Name';
2382+
const cmpFunc = (obj1, obj2) => this.compareObjects(obj1, obj2, key);
2383+
return data.sort(cmpFunc);
2384+
}
2385+
2386+
protected compareObjects(obj1, obj2, key: string) {
2387+
const a = obj1[key].toLowerCase();
2388+
const b = obj2[key].toLowerCase();
2389+
return a > b ? 1 : a < b ? -1 : 0;
2390+
}
2391+
}
2392+
23692393
@Component({
23702394
template: `
23712395
<igx-grid #grid [data]="data" [height]="'500px'" [width]="'500px'">

0 commit comments

Comments
 (0)