Skip to content

Commit 0e094c9

Browse files
author
Rhathe
committed
feat(sort): Give more information to the sort functions
Sort functions are now passed additional rowA, rowB, and direction parameters
1 parent 5cbaf5f commit 0e094c9

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

misc/tutorial/102_sorting.ngdoc

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ asynchronously after the columns it will often decide all your columns are strin
3434
column def using `type='number'`. Valid types are documented in {@link api/ui.grid.class:GridOptions.columnDef columnDef}, and
3535
include `string`, `number`, `numberStr` and `date`. If you use date be aware the code expects a javascript date object.
3636

37+
You can pass in a custom sorting algorithm to a column by setting the
38+
{@link api/ui.grid.class:GridOptions.columnDef#sortingAlgorithm sortingAlgorithm} columnDef option.
39+
The sorting algorithm function takes 'a' and 'b' parameters like any normal sorting function with additional
40+
'rowA', 'rowB', and 'direction' parameters that are the row objects and the current direction of the sort respectively.
41+
3742
By default the sorting algorithm will be applied to the row value before any `cellFilters` are applied. The {@link api/ui.grid.class:GridOptions.columnDef#sortCellFiltered sortCellFiltered}
3843
columnDef option will cause sorting to be applied after the `cellFilters` are applied. For an example of this see the "Month Joined" column in the {@link 401_AllFeatures AllFeatures tutorial}.
3944

@@ -83,7 +88,7 @@ columnDef option will cause sorting to be applied after the `cellFilters` are ap
8388
priority: 0,
8489
},
8590
suppressRemoveSort: true,
86-
sortingAlgorithm: function(a, b) {
91+
sortingAlgorithm: function(a, b, rowA, rowB, direction) {
8792
var nulls = $scope.grid2Api.core.sortHandleNulls(a, b);
8893
if( nulls !== null ) {
8994
return nulls;

src/js/core/factories/GridColumn.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,13 @@ angular.module('ui.grid')
295295
*/
296296

297297

298-
/**
299-
* @ngdoc property
300-
* @name sortingAlgorithm
301-
* @propertyOf ui.grid.class:GridColumn
302-
* @description Algorithm to use for sorting this column. Takes 'a' and 'b' parameters
303-
* like any normal sorting function.
304-
*
305-
*/
306-
307298
/**
308299
* @ngdoc property
309300
* @name sortingAlgorithm
310301
* @propertyOf ui.grid.class:GridOptions.columnDef
311302
* @description Algorithm to use for sorting this column. Takes 'a' and 'b' parameters
312-
* like any normal sorting function.
303+
* like any normal sorting function with additional 'rowA', 'rowB', and 'direction' parameters
304+
* that are the row objects and the current direction of the sort respectively.
313305
*
314306
*/
315307

src/js/core/services/rowSorter.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
418418
if (sortCols.length === 0) {
419419
return rows;
420420
}
421-
421+
422422
// Re-usable variables
423423
var col, direction;
424424

@@ -444,7 +444,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
444444
direction = sortCols[idx].sort.direction;
445445

446446
sortFn = rowSorter.getSortFn(grid, col, r);
447-
447+
448448
var propA, propB;
449449

450450
if ( col.sortCellFiltered ){
@@ -455,7 +455,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
455455
propB = grid.getCellValue(rowB, col);
456456
}
457457

458-
tem = sortFn(propA, propB);
458+
tem = sortFn(propA, propB, rowA, rowB, direction);
459459

460460
idx++;
461461
}
@@ -467,7 +467,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
467467
if (tem === 0 ) {
468468
return rowA.entity.$$uiGridIndex - rowB.entity.$$uiGridIndex;
469469
}
470-
470+
471471
// Made it this far, we don't have to worry about null & undefined
472472
if (direction === uiGridConstants.ASC) {
473473
return tem;

0 commit comments

Comments
 (0)