Skip to content

Commit 62dbcfe

Browse files
committed
fix(core): Sort Priority Zero Based
Before sort priority was a mix of 0 and 1 based. You could create columns with a sort priority of zero but once the user clicked on a column to sort it its index would change to 1. There was no way for the sort index to return to 0 from within the grid's menus. This mix of 0 and 1 based sorting was confusing and created UI problems when displaying the sort index in the column header. Closes #4685 BREAKING CHANGE: **GridOptions.columnDef.sort.priority** now expects the lowest value to be 0. The Grid Header will display a sort priority of 0 as 1. Using `if(col.sort.priority)` to determine if a column is sorted is no longer valid as `0 == false`. Saved grid objects may be affected by this.
1 parent cecb7eb commit 62dbcfe

File tree

5 files changed

+101
-101
lines changed

5 files changed

+101
-101
lines changed

src/features/saveState/test/saveState.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ describe('ui.grid.saveState uiGridSaveStateService', function () {
409409

410410
expect( onSortChangedHook.calls.length ).toEqual( 1 );
411411

412-
expect( onSortChangedHook ).toHaveBeenCalledWith(
413-
grid,
414-
[ grid.getOnlyDataColumns()[3], grid.getOnlyDataColumns()[2] ]
412+
expect( onSortChangedHook ).toHaveBeenCalledWith(
413+
grid,
414+
[ grid.getOnlyDataColumns()[2], grid.getOnlyDataColumns()[3] ]
415415
);
416416
});
417417

src/js/core/factories/Grid.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ angular.module('ui.grid')
397397
* @methodOf ui.grid.core.api:PublicApi
398398
* @description The visibility of a column has changed,
399399
* the column itself is passed out as a parameter of the event.
400-
*
400+
*
401401
* @param {$scope} scope The scope of the controller. This is used to deregister this event when the scope is destroyed.
402402
* @param {Function} callBack Will be called when the event is emited. The function passes back the GridCol that has changed.
403403
*
@@ -1087,17 +1087,17 @@ angular.module('ui.grid')
10871087
* append to the newRows and add to newHash
10881088
* run the processors
10891089
* ```
1090-
*
1090+
*
10911091
* Rows are identified using the hashKey if configured. If not configured, then rows
10921092
* are identified using the gridOptions.rowEquality function
1093-
*
1093+
*
10941094
* This method is useful when trying to select rows immediately after loading data without
10951095
* using a $timeout/$interval, e.g.:
1096-
*
1096+
*
10971097
* $scope.gridOptions.data = someData;
10981098
* $scope.gridApi.grid.modifyRows($scope.gridOptions.data);
10991099
* $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
1100-
*
1100+
*
11011101
* OR to persist row selection after data update (e.g. rows selected, new data loaded, want
11021102
* originally selected rows to be re-selected))
11031103
*/
@@ -1876,12 +1876,12 @@ angular.module('ui.grid')
18761876
p = 0;
18771877

18781878
self.columns.forEach(function (col) {
1879-
if (col.sort && col.sort.priority && col.sort.priority > p) {
1880-
p = col.sort.priority;
1879+
if (col.sort && col.sort.priority !== undefined && col.sort.priority >= p) {
1880+
p = col.sort.priority + 1;
18811881
}
18821882
});
18831883

1884-
return p + 1;
1884+
return p;
18851885
};
18861886

18871887
/**
@@ -1960,7 +1960,7 @@ angular.module('ui.grid')
19601960

19611961
if (!add) {
19621962
self.resetColumnSorting(column);
1963-
column.sort.priority = 0;
1963+
column.sort.priority = undefined;
19641964
// Get the actual priority since there may be columns which have suppressRemoveSort set
19651965
column.sort.priority = self.getNextColumnSortPriority();
19661966
}

src/js/core/services/rowSorter.js

+36-36
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ var module = angular.module('ui.grid');
55
/**
66
* @ngdoc object
77
* @name ui.grid.class:RowSorter
8-
* @description RowSorter provides the default sorting mechanisms,
9-
* including guessing column types and applying appropriate sort
8+
* @description RowSorter provides the default sorting mechanisms,
9+
* including guessing column types and applying appropriate sort
1010
* algorithms
11-
*
12-
*/
11+
*
12+
*/
1313

1414
module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGridConstants) {
15-
var currencyRegexStr =
15+
var currencyRegexStr =
1616
'(' +
1717
uiGridConstants.CURRENCY_SYMBOLS
1818
.map(function (a) { return '\\' + a; }) // Escape all the currency symbols ($ at least will jack up this regex)
@@ -95,7 +95,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
9595
* @methodOf ui.grid.class:RowSorter
9696
* @name basicSort
9797
* @description Sorts any values that provide the < method, including strings
98-
* or numbers. Handles nulls and undefined through calling handleNulls
98+
* or numbers. Handles nulls and undefined through calling handleNulls
9999
* @param {object} a sort value a
100100
* @param {object} b sort value b
101101
* @returns {number} normal sort function, returns -ve, 0, +ve
@@ -120,7 +120,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
120120
* @ngdoc method
121121
* @methodOf ui.grid.class:RowSorter
122122
* @name sortNumber
123-
* @description Sorts numerical values. Handles nulls and undefined through calling handleNulls
123+
* @description Sorts numerical values. Handles nulls and undefined through calling handleNulls
124124
* @param {object} a sort value a
125125
* @param {object} b sort value b
126126
* @returns {number} normal sort function, returns -ve, 0, +ve
@@ -139,8 +139,8 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
139139
* @ngdoc method
140140
* @methodOf ui.grid.class:RowSorter
141141
* @name sortNumberStr
142-
* @description Sorts numerical values that are stored in a string (i.e. parses them to numbers first).
143-
* Handles nulls and undefined through calling handleNulls
142+
* @description Sorts numerical values that are stored in a string (i.e. parses them to numbers first).
143+
* Handles nulls and undefined through calling handleNulls
144144
* @param {object} a sort value a
145145
* @param {object} b sort value b
146146
* @returns {number} normal sort function, returns -ve, 0, +ve
@@ -154,36 +154,36 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
154154
numB, // The parsed number form of 'b'
155155
badA = false,
156156
badB = false;
157-
157+
158158
// Try to parse 'a' to a float
159159
numA = parseFloat(a.replace(/[^0-9.-]/g, ''));
160-
160+
161161
// If 'a' couldn't be parsed to float, flag it as bad
162162
if (isNaN(numA)) {
163163
badA = true;
164164
}
165-
165+
166166
// Try to parse 'b' to a float
167167
numB = parseFloat(b.replace(/[^0-9.-]/g, ''));
168-
168+
169169
// If 'b' couldn't be parsed to float, flag it as bad
170170
if (isNaN(numB)) {
171171
badB = true;
172172
}
173-
173+
174174
// We want bad ones to get pushed to the bottom... which effectively is "greater than"
175175
if (badA && badB) {
176176
return 0;
177177
}
178-
178+
179179
if (badA) {
180180
return 1;
181181
}
182-
182+
183183
if (badB) {
184184
return -1;
185185
}
186-
186+
187187
return numA - numB;
188188
}
189189
};
@@ -193,7 +193,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
193193
* @ngdoc method
194194
* @methodOf ui.grid.class:RowSorter
195195
* @name sortAlpha
196-
* @description Sorts string values. Handles nulls and undefined through calling handleNulls
196+
* @description Sorts string values. Handles nulls and undefined through calling handleNulls
197197
* @param {object} a sort value a
198198
* @param {object} b sort value b
199199
* @returns {number} normal sort function, returns -ve, 0, +ve
@@ -205,7 +205,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
205205
} else {
206206
var strA = a.toString().toLowerCase(),
207207
strB = b.toString().toLowerCase();
208-
208+
209209
return strA === strB ? 0 : strA.localeCompare(strB);
210210
}
211211
};
@@ -234,7 +234,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
234234
}
235235
var timeA = a.getTime(),
236236
timeB = b.getTime();
237-
237+
238238
return timeA === timeB ? 0 : (timeA < timeB ? -1 : 1);
239239
}
240240
};
@@ -244,8 +244,8 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
244244
* @ngdoc method
245245
* @methodOf ui.grid.class:RowSorter
246246
* @name sortBool
247-
* @description Sorts boolean values, true is considered larger than false.
248-
* Handles nulls and undefined through calling handleNulls
247+
* @description Sorts boolean values, true is considered larger than false.
248+
* Handles nulls and undefined through calling handleNulls
249249
* @param {object} a sort value a
250250
* @param {object} b sort value b
251251
* @returns {number} normal sort function, returns -ve, 0, +ve
@@ -258,7 +258,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
258258
if (a && b) {
259259
return 0;
260260
}
261-
261+
262262
if (!a && !b) {
263263
return 0;
264264
}
@@ -273,17 +273,17 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
273273
* @ngdoc method
274274
* @methodOf ui.grid.class:RowSorter
275275
* @name getSortFn
276-
* @description Get the sort function for the column. Looks first in
276+
* @description Get the sort function for the column. Looks first in
277277
* rowSorter.colSortFnCache using the column name, failing that it
278278
* looks at col.sortingAlgorithm (and puts it in the cache), failing that
279279
* it guesses the sort algorithm based on the data type.
280-
*
280+
*
281281
* The cache currently seems a bit pointless, as none of the work we do is
282282
* processor intensive enough to need caching. Presumably in future we might
283283
* inspect the row data itself to guess the sort function, and in that case
284284
* it would make sense to have a cache, the infrastructure is in place to allow
285285
* that.
286-
*
286+
*
287287
* @param {Grid} grid the grid to consider
288288
* @param {GridCol} col the column to find a function for
289289
* @param {array} rows an array of grid rows. Currently unused, but presumably in future
@@ -336,7 +336,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
336336
* @description Used where multiple columns are present in the sort criteria,
337337
* we determine which column should take precedence in the sort by sorting
338338
* the columns based on their sort.priority
339-
*
339+
*
340340
* @param {gridColumn} a column a
341341
* @param {gridColumn} b column b
342342
* @returns {number} normal sort function, returns -ve, 0, +ve
@@ -358,11 +358,11 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
358358
}
359359
}
360360
// Only A has a priority
361-
else if (a.sort.priority || a.sort.priority === 0) {
361+
else if (a.sort.priority || a.sort.priority === undefined) {
362362
return -1;
363363
}
364364
// Only B has a priority
365-
else if (b.sort.priority || b.sort.priority === 0) {
365+
else if (b.sort.priority || b.sort.priority === undefined) {
366366
return 1;
367367
}
368368
// Neither has a priority
@@ -379,14 +379,14 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
379379
* @description Prevents the internal sorting from executing. Events will
380380
* still be fired when the sort changes, and the sort information on
381381
* the columns will be updated, allowing an external sorter (for example,
382-
* server sorting) to be implemented. Defaults to false.
383-
*
382+
* server sorting) to be implemented. Defaults to false.
383+
*
384384
*/
385385
/**
386386
* @ngdoc method
387387
* @methodOf ui.grid.class:RowSorter
388388
* @name sort
389-
* @description sorts the grid
389+
* @description sorts the grid
390390
* @param {Object} grid the grid itself
391391
* @param {array} rows the rows to be sorted
392392
* @param {array} columns the columns in which to look
@@ -398,7 +398,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
398398
if (!rows) {
399399
return;
400400
}
401-
401+
402402
if (grid.options.useExternalSorting){
403403
return rows;
404404
}
@@ -460,7 +460,7 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
460460
idx++;
461461
}
462462

463-
// Chrome doesn't implement a stable sort function. If our sort returns 0
463+
// Chrome doesn't implement a stable sort function. If our sort returns 0
464464
// (i.e. the items are equal), and we're at the last sort column in the list,
465465
// then return the previous order using our custom
466466
// index variable
@@ -477,13 +477,13 @@ module.service('rowSorter', ['$parse', 'uiGridConstants', function ($parse, uiGr
477477
};
478478

479479
var newRows = rows.sort(rowSortFn);
480-
480+
481481
// remove the custom index field on each row, used to make a stable sort out of unstable sorts (e.g. Chrome)
482482
var clearIndex = function( row, idx ){
483483
delete row.entity.$$uiGridIndex;
484484
};
485485
rows.forEach(clearIndex);
486-
486+
487487
return newRows;
488488
};
489489

src/templates/ui-grid/uiGridHeaderCell.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
aria-label="{{getSortDirectionAriaLabel()}}">
2222
<i
2323
ng-class="{ 'ui-grid-icon-up-dir': col.sort.direction == asc, 'ui-grid-icon-down-dir': col.sort.direction == desc, 'ui-grid-icon-blank': !col.sort.direction }"
24-
title="{{isSortPriorityVisible() ? i18n.headerCell.priority + ' ' + col.sort.priority : null}}"
24+
title="{{isSortPriorityVisible() ? i18n.headerCell.priority + ' ' + ( col.sort.priority + 1 ) : null}}"
2525
aria-hidden="true">
2626
</i>
2727
<sub
2828
ui-grid-visible="isSortPriorityVisible()"
2929
class="ui-grid-sort-priority-number">
30-
{{col.sort.priority}}
30+
{{col.sort.priority + 1}}
3131
</sub>
3232
</span>
3333
</div>

0 commit comments

Comments
 (0)