From 0e094c97fc55d064150e1a33a3042c5091327be3 Mon Sep 17 00:00:00 2001 From: Rhathe Date: Thu, 1 Oct 2015 14:43:36 -0400 Subject: [PATCH] feat(sort): Give more information to the sort functions Sort functions are now passed additional rowA, rowB, and direction parameters --- misc/tutorial/102_sorting.ngdoc | 7 ++++++- src/js/core/factories/GridColumn.js | 12 ++---------- src/js/core/services/rowSorter.js | 8 ++++---- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/misc/tutorial/102_sorting.ngdoc b/misc/tutorial/102_sorting.ngdoc index bf6325f819..7789966494 100644 --- a/misc/tutorial/102_sorting.ngdoc +++ b/misc/tutorial/102_sorting.ngdoc @@ -34,6 +34,11 @@ asynchronously after the columns it will often decide all your columns are strin column def using `type='number'`. Valid types are documented in {@link api/ui.grid.class:GridOptions.columnDef columnDef}, and include `string`, `number`, `numberStr` and `date`. If you use date be aware the code expects a javascript date object. +You can pass in a custom sorting algorithm to a column by setting the +{@link api/ui.grid.class:GridOptions.columnDef#sortingAlgorithm sortingAlgorithm} columnDef option. +The sorting algorithm function takes 'a' and 'b' parameters like any normal sorting function with additional +'rowA', 'rowB', and 'direction' parameters that are the row objects and the current direction of the sort respectively. + 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} 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}. @@ -83,7 +88,7 @@ columnDef option will cause sorting to be applied after the `cellFilters` are ap priority: 0, }, suppressRemoveSort: true, - sortingAlgorithm: function(a, b) { + sortingAlgorithm: function(a, b, rowA, rowB, direction) { var nulls = $scope.grid2Api.core.sortHandleNulls(a, b); if( nulls !== null ) { return nulls; diff --git a/src/js/core/factories/GridColumn.js b/src/js/core/factories/GridColumn.js index 95004eb585..21ee0c42e7 100644 --- a/src/js/core/factories/GridColumn.js +++ b/src/js/core/factories/GridColumn.js @@ -295,21 +295,13 @@ angular.module('ui.grid') */ - /** - * @ngdoc property - * @name sortingAlgorithm - * @propertyOf ui.grid.class:GridColumn - * @description Algorithm to use for sorting this column. Takes 'a' and 'b' parameters - * like any normal sorting function. - * - */ - /** * @ngdoc property * @name sortingAlgorithm * @propertyOf ui.grid.class:GridOptions.columnDef * @description Algorithm to use for sorting this column. Takes 'a' and 'b' parameters - * like any normal sorting function. + * like any normal sorting function with additional 'rowA', 'rowB', and 'direction' parameters + * that are the row objects and the current direction of the sort respectively. * */ diff --git a/src/js/core/services/rowSorter.js b/src/js/core/services/rowSorter.js index bc29ce1e96..6d4471709a 100644 --- a/src/js/core/services/rowSorter.js +++ b/src/js/core/services/rowSorter.js @@ -418,7 +418,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr if (sortCols.length === 0) { return rows; } - + // Re-usable variables var col, direction; @@ -444,7 +444,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr direction = sortCols[idx].sort.direction; sortFn = rowSorter.getSortFn(grid, col, r); - + var propA, propB; if ( col.sortCellFiltered ){ @@ -455,7 +455,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr propB = grid.getCellValue(rowB, col); } - tem = sortFn(propA, propB); + tem = sortFn(propA, propB, rowA, rowB, direction); idx++; } @@ -467,7 +467,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr if (tem === 0 ) { return rowA.entity.$$uiGridIndex - rowB.entity.$$uiGridIndex; } - + // Made it this far, we don't have to worry about null & undefined if (direction === uiGridConstants.ASC) { return tem;