diff --git a/projects/igniteui-angular/src/lib/grids/grid/grid.sorting.spec.ts b/projects/igniteui-angular/src/lib/grids/grid/grid.sorting.spec.ts index 5585bc05b1f..ad6cae90d03 100644 --- a/projects/igniteui-angular/src/lib/grids/grid/grid.sorting.spec.ts +++ b/projects/igniteui-angular/src/lib/grids/grid/grid.sorting.spec.ts @@ -7,7 +7,8 @@ import { IgxGridModule } from './index'; import { GridTemplateStrings, ColumnDefinitions } from '../../test-utils/template-strings.spec'; import { BasicGridComponent } from '../../test-utils/grid-base-components.spec'; import { SampleTestData } from '../../test-utils/sample-test-data.spec'; -import { DefaultSortingStrategy } from '../../data-operations/sorting-strategy'; +import { DefaultSortingStrategy, ISortingStrategy } from '../../data-operations/sorting-strategy'; +import { IgxGridCellComponent } from '../cell.component'; import { configureTestSuite } from '../../test-utils/configure-suite'; const SORTING_ICON_NONE_CONTENT = 'none'; @@ -317,6 +318,38 @@ describe('IgxGrid - Grid Sorting', () => { }); }); +describe('IgxGrid - Grid Custom Sorting', () => { + let fixture; + let grid: IgxGridComponent; + it(`Should allow sorting using a custom Sorting Strategy.`, () => { + TestBed.configureTestingModule({ + declarations: [SortByParityComponent], + imports: [IgxGridModule.forRoot()] + }).compileComponents(); + fixture = TestBed.createComponent(SortByParityComponent); + grid = fixture.componentInstance.grid; + fixture.componentInstance.data.push( + { ID: 8, Name: 'Brad', LastName: 'Walker', Region: 'DD' }, + { ID: 9, Name: 'Mary', LastName: 'Smith', Region: 'OC' }, + { ID: 10, Name: 'Brad', LastName: 'Smith', Region: 'BD' }, + ); + fixture.detectChanges(); + grid.sort({ + fieldName: 'ID', + dir: SortingDirection.Desc, + ignoreCase: false, + strategy: new SortByParityComponent() + }); + fixture.detectChanges(); + const oddHalf: IgxGridCellComponent[] = grid.getColumnByName('ID').cells.slice(0, 5); + const evenHalf: IgxGridCellComponent[] = grid.getColumnByName('ID').cells.slice(5); + const isFirstHalfOdd: boolean = oddHalf.every(cell => cell.value % 2 === 1); + const isSecondHalfEven: boolean = evenHalf.every(cell => cell.value % 2 === 0); + expect(isFirstHalfOdd).toEqual(true); + expect(isSecondHalfEven).toEqual(true); + }); +}); + @Component({ template: GridTemplateStrings.declareGrid( '', @@ -333,6 +366,32 @@ export class GridDeclaredColumnsComponent extends BasicGridComponent { public width = '800px'; } +@Component({ + template: GridTemplateStrings.declareGrid( + '', + '', + ColumnDefinitions.idFirstLastNameSortable) +}) +export class SortByParityComponent extends GridDeclaredColumnsComponent implements ISortingStrategy { + public sort(data: any[], fieldName: string, dir: SortingDirection) { + const key = fieldName; + const reverse = (dir === SortingDirection.Desc ? -1 : 1); + const cmpFunc = (obj1, obj2) => { + return this.compareObjects(obj1, obj2, key, reverse); + }; + return data.sort(cmpFunc); + } + protected sortByParity(a: any, b: any) { + return a % 2 === 0 ? -1 : b % 2 === 0 ? 1 : 0; + } + protected compareObjects(obj1: object, obj2: object, key: string, reverse: number) { + const a = obj1[key]; + const b = obj2[key]; + return reverse * this.sortByParity(a, b); + } +} + + function getCurrentCellFromGrid(grid, row, cell) { const gridRow = grid.rowList.toArray()[row]; const gridCell = gridRow.cells.toArray()[cell];