Skip to content

Commit

Permalink
feat: 🎸 option to disable multi-column sorting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marcelo-portugal authored and mportuga committed Aug 4, 2021
1 parent 67f6fe2 commit c9abb8b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
6 changes: 5 additions & 1 deletion misc/tutorial/102_sorting.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ UI-Grid allows you to sort rows. The feature is on by default. You can set the `
Sorting can be disabled at the column level by setting `enableSorting: false` in the column def. See the last column below for an example.

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

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

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

vm.gridOptions2 = {
enableSorting: true,
suppressMultiSort: true,
onRegisterApi: function( gridApi ) {
vm.grid2Api = gridApi;
},
Expand Down Expand Up @@ -122,7 +126,7 @@ For better performance with the following example, you can choose to load the ui
}
}
},
{ field: 'company', enableSorting: false }
{ field: 'company', enableSorting: true }
]
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/js/factories/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ angular.module('ui.grid')
direction = directionOrAdd;
}

if (!add) {
if (!add || (self.options && self.options.suppressMultiSort)) {
self.resetColumnSorting(column);
column.sort.priority = undefined;
// Get the actual priority since there may be columns which have suppressRemoveSort set
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/js/factories/GridOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ angular.module('ui.grid')
*/
baseOptions.enableSorting = baseOptions.enableSorting !== false;

/**
* @ngdoc boolean
* @name suppressMultiSort
* @propertyOf ui.grid.class:GridOptions
* @description False by default. When enabled, this setting disables the ability
* to sort multiple columns by using the shift key or interacting with the column
* menu. Instead, each column sort will remove all other sorting.
*/
baseOptions.suppressMultiSort = baseOptions.suppressMultiSort === true;

/**
* @ngdoc boolean
* @name enableFiltering
Expand Down
27 changes: 27 additions & 0 deletions packages/core/test/core/factories/Grid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,10 @@ describe('Grid factory', function() {
});

describe('sortColumn', function() {
beforeEach(function() {
grid.options.suppressMultiSort = false;
});

it('should throw an exception if no column parameter is provided', function() {
expect(function() {
grid.sortColumn();
Expand Down Expand Up @@ -1060,6 +1064,17 @@ describe('Grid factory', function() {
expect(priorColumn.sort).toEqual({});
});

it('if another column has a sort, and both add and suppressMultiSort are set to true, that sort should be removed', function() {
var priorColumn = new GridColumn({name: 'b', sort: {direction: uiGridConstants.ASC}}, 0, grid);
grid.columns.push(priorColumn);
grid.options.suppressMultiSort = true;
grid.sortColumn(column, true);

expect(column.sort.direction).toEqual(uiGridConstants.ASC);
expect(column.sort.priority).toEqual(0);
expect(priorColumn.sort).toEqual({});
});

it('if another column has a sort, and add is set to true, then that sort should not be removed', function() {
var priorColumn = new GridColumn({name: 'b', sort: {direction: uiGridConstants.ASC, priority: 1}}, 0, grid);
grid.columns.push(priorColumn);
Expand All @@ -1083,6 +1098,18 @@ describe('Grid factory', function() {
expect(priorColumn.sort).toEqual({direction: uiGridConstants.ASC, priority: 1});
});

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',
function() {
var priorColumn = new GridColumn({name: 'b', sort: {direction: uiGridConstants.ASC, priority: 1}, suppressRemoveSort: true}, 0, grid);
grid.columns.push(priorColumn);
grid.options.suppressMultiSort = true;
grid.sortColumn(column, true);

expect(column.sort.direction).toEqual(uiGridConstants.ASC);
expect(column.sort.priority).toEqual(2);
expect(priorColumn.sort).toEqual({direction: uiGridConstants.ASC, priority: 1});
});

it('if sortDirectionCycle is null-DESC-ASC, and sort is currently null, then should toggle to DESC, and reset priority', function() {
column.sort = {};
column.sortDirectionCycle = [null, uiGridConstants.DESC, uiGridConstants.ASC];
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/core/factories/GridOptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('GridOptions factory', function () {
scrollDebounce: 300,
enableHiding: true,
enableSorting: true,
suppressMultiSort: false,
enableFiltering: false,
filterContainer: 'headerCell',
enableColumnMenus: true,
Expand Down Expand Up @@ -89,6 +90,7 @@ describe('GridOptions factory', function () {
wheelScrollThrottle: 75,
enableHiding: true,
enableSorting: true,
suppressMultiSort: true,
enableFiltering: true,
filterContainer: 'columnMenu',
enableColumnMenus: true,
Expand Down Expand Up @@ -136,6 +138,7 @@ describe('GridOptions factory', function () {
scrollDebounce: 300,
enableHiding: true,
enableSorting: true,
suppressMultiSort: true,
enableFiltering: true,
filterContainer: 'columnMenu',
enableColumnMenus: true,
Expand Down Expand Up @@ -188,6 +191,7 @@ describe('GridOptions factory', function () {
filterContainer: 'columnMenu',
enableHiding: false,
enableSorting: false,
suppressMultiSort: false,
enableColumnMenus: false,
enableVerticalScrollbar: 0,
enableHorizontalScrollbar: 0,
Expand Down Expand Up @@ -232,6 +236,7 @@ describe('GridOptions factory', function () {
scrollDebounce: 300,
enableHiding: false,
enableSorting: false,
suppressMultiSort: false,
enableFiltering: false,
filterContainer: 'columnMenu',
enableColumnMenus: false,
Expand Down

0 comments on commit c9abb8b

Please sign in to comment.