Skip to content

Commit c9abb8b

Browse files
marcelo-portugalmportuga
authored andcommitted
feat: 🎸 option to disable multi-column sorting
Adds a new grid option `suppressMultiSort`, which when ensures that when you sort one column, all others should be reset to no sort (as long as they don't have suppressRemoveSort set to true). Closes: #2913
1 parent 67f6fe2 commit c9abb8b

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

Diff for: ‎misc/tutorial/102_sorting.ngdoc

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ UI-Grid allows you to sort rows. The feature is on by default. You can set the `
1111
Sorting can be disabled at the column level by setting `enableSorting: false` in the column def. See the last column below for an example.
1212

1313
Multiple columns can be sorted by shift-clicking on the 2-n columns. To see it in action, sort Gender then shift-click Name.
14+
This feature can be disabled by setting `suppressMultiSort: true`. When combined with the `suppressRemoveSort`, it allows you to at most sort one extra
15+
column beyond those with suppressRemoveSort turned on. This way, any column that is meant to stay sorted will not be modified.
1416

1517
When sorting using the menus, the sorts are additive. So if you have one column sorted and you pick another sort
1618
from a column menu, it will add on to the existing sort rather than replacing it. You need to use the 'remove sort' option
@@ -59,6 +61,7 @@ For better performance with the following example, you can choose to load the ui
5961

6062
vm.gridOptions1 = {
6163
enableSorting: true,
64+
suppressMultiSort: true,
6265
columnDefs: [
6366
{ field: 'name' },
6467
{ field: 'gender' },
@@ -80,6 +83,7 @@ For better performance with the following example, you can choose to load the ui
8083

8184
vm.gridOptions2 = {
8285
enableSorting: true,
86+
suppressMultiSort: true,
8387
onRegisterApi: function( gridApi ) {
8488
vm.grid2Api = gridApi;
8589
},
@@ -122,7 +126,7 @@ For better performance with the following example, you can choose to load the ui
122126
}
123127
}
124128
},
125-
{ field: 'company', enableSorting: false }
129+
{ field: 'company', enableSorting: true }
126130
]
127131
};
128132

Diff for: ‎packages/core/src/js/factories/Grid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ angular.module('ui.grid')
19571957
direction = directionOrAdd;
19581958
}
19591959

1960-
if (!add) {
1960+
if (!add || (self.options && self.options.suppressMultiSort)) {
19611961
self.resetColumnSorting(column);
19621962
column.sort.priority = undefined;
19631963
// Get the actual priority since there may be columns which have suppressRemoveSort set

Diff for: ‎packages/core/src/js/factories/GridOptions.js

+10
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,16 @@ angular.module('ui.grid')
388388
*/
389389
baseOptions.enableSorting = baseOptions.enableSorting !== false;
390390

391+
/**
392+
* @ngdoc boolean
393+
* @name suppressMultiSort
394+
* @propertyOf ui.grid.class:GridOptions
395+
* @description False by default. When enabled, this setting disables the ability
396+
* to sort multiple columns by using the shift key or interacting with the column
397+
* menu. Instead, each column sort will remove all other sorting.
398+
*/
399+
baseOptions.suppressMultiSort = baseOptions.suppressMultiSort === true;
400+
391401
/**
392402
* @ngdoc boolean
393403
* @name enableFiltering

Diff for: ‎packages/core/test/core/factories/Grid.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,10 @@ describe('Grid factory', function() {
988988
});
989989

990990
describe('sortColumn', function() {
991+
beforeEach(function() {
992+
grid.options.suppressMultiSort = false;
993+
});
994+
991995
it('should throw an exception if no column parameter is provided', function() {
992996
expect(function() {
993997
grid.sortColumn();
@@ -1060,6 +1064,17 @@ describe('Grid factory', function() {
10601064
expect(priorColumn.sort).toEqual({});
10611065
});
10621066

1067+
it('if another column has a sort, and both add and suppressMultiSort are set to true, that sort should be removed', function() {
1068+
var priorColumn = new GridColumn({name: 'b', sort: {direction: uiGridConstants.ASC}}, 0, grid);
1069+
grid.columns.push(priorColumn);
1070+
grid.options.suppressMultiSort = true;
1071+
grid.sortColumn(column, true);
1072+
1073+
expect(column.sort.direction).toEqual(uiGridConstants.ASC);
1074+
expect(column.sort.priority).toEqual(0);
1075+
expect(priorColumn.sort).toEqual({});
1076+
});
1077+
10631078
it('if another column has a sort, and add is set to true, then that sort should not be removed', function() {
10641079
var priorColumn = new GridColumn({name: 'b', sort: {direction: uiGridConstants.ASC, priority: 1}}, 0, grid);
10651080
grid.columns.push(priorColumn);
@@ -1083,6 +1098,18 @@ describe('Grid factory', function() {
10831098
expect(priorColumn.sort).toEqual({direction: uiGridConstants.ASC, priority: 1});
10841099
});
10851100

1101+
it('if another column has a sort, and both add and suppressMultiSort are set to true, but that other column has suppressRemoveSort, then it shouldn\'t be removed',
1102+
function() {
1103+
var priorColumn = new GridColumn({name: 'b', sort: {direction: uiGridConstants.ASC, priority: 1}, suppressRemoveSort: true}, 0, grid);
1104+
grid.columns.push(priorColumn);
1105+
grid.options.suppressMultiSort = true;
1106+
grid.sortColumn(column, true);
1107+
1108+
expect(column.sort.direction).toEqual(uiGridConstants.ASC);
1109+
expect(column.sort.priority).toEqual(2);
1110+
expect(priorColumn.sort).toEqual({direction: uiGridConstants.ASC, priority: 1});
1111+
});
1112+
10861113
it('if sortDirectionCycle is null-DESC-ASC, and sort is currently null, then should toggle to DESC, and reset priority', function() {
10871114
column.sort = {};
10881115
column.sortDirectionCycle = [null, uiGridConstants.DESC, uiGridConstants.ASC];

Diff for: ‎packages/core/test/core/factories/GridOptions.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('GridOptions factory', function () {
4040
scrollDebounce: 300,
4141
enableHiding: true,
4242
enableSorting: true,
43+
suppressMultiSort: false,
4344
enableFiltering: false,
4445
filterContainer: 'headerCell',
4546
enableColumnMenus: true,
@@ -89,6 +90,7 @@ describe('GridOptions factory', function () {
8990
wheelScrollThrottle: 75,
9091
enableHiding: true,
9192
enableSorting: true,
93+
suppressMultiSort: true,
9294
enableFiltering: true,
9395
filterContainer: 'columnMenu',
9496
enableColumnMenus: true,
@@ -136,6 +138,7 @@ describe('GridOptions factory', function () {
136138
scrollDebounce: 300,
137139
enableHiding: true,
138140
enableSorting: true,
141+
suppressMultiSort: true,
139142
enableFiltering: true,
140143
filterContainer: 'columnMenu',
141144
enableColumnMenus: true,
@@ -188,6 +191,7 @@ describe('GridOptions factory', function () {
188191
filterContainer: 'columnMenu',
189192
enableHiding: false,
190193
enableSorting: false,
194+
suppressMultiSort: false,
191195
enableColumnMenus: false,
192196
enableVerticalScrollbar: 0,
193197
enableHorizontalScrollbar: 0,
@@ -232,6 +236,7 @@ describe('GridOptions factory', function () {
232236
scrollDebounce: 300,
233237
enableHiding: false,
234238
enableSorting: false,
239+
suppressMultiSort: false,
235240
enableFiltering: false,
236241
filterContainer: 'columnMenu',
237242
enableColumnMenus: false,

0 commit comments

Comments
 (0)