Skip to content

Commit 3d4e857

Browse files
committed
Merge pull request #1711 from mathiasdose/feature1211
Issue #1211 Multi select when using Ctrl-Key and Shift-Key
2 parents 26c9c91 + 0b09e61 commit 3d4e857

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

misc/tutorial/210_selection.ngdoc

+24-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ can select a different row (which will unselect the current row), but you cannot
1919
by clicking on it. This means that at least one row is always selected, other than when you first open the grid. If
2020
necessary you could programatically select the first row upon page open. The second grid demonstrates this.
2121

22+
If `multiSelect: true`, another option `modifierKeysToMultiSelect` may be used. If set to true will allow selecting multiple
23+
row only if the Ctrl-Key, Cmd-Key (Mac) or the Shift-Key is used when selecting, set to false will allow selecting multiple
24+
rows always.
25+
2226
@example
2327
Two examples are provided, the first with rowHeaderSelection and multi-select, the second without.
2428

@@ -46,19 +50,23 @@ Two examples are provided, the first with rowHeaderSelection and multi-select, t
4650
$scope.info = {};
4751

4852
$scope.toggleMultiSelect = function() {
49-
$scope.gridApi.selection.setMultiSelect(!$scope.gridApi.grid.options.multiSelect);
53+
$scope.gridApi.selection.setMultiSelect(!$scope.gridApi.grid.options.multiSelect);
54+
};
55+
56+
$scope.toggleModifierKeysToMultiSelect = function() {
57+
$scope.gridApi.selection.setModifierKeysToMultiSelect(!$scope.gridApi.grid.options.modifierKeysToMultiSelect);
5058
};
5159

5260
$scope.selectAll = function() {
53-
$scope.gridApi.selection.selectAllRows();
61+
$scope.gridApi.selection.selectAllRows();
5462
};
5563

56-
$scope.clearAll = function() {
57-
$scope.gridApi.selection.clearSelectedRows();
58-
};
64+
$scope.clearAll = function() {
65+
$scope.gridApi.selection.clearSelectedRows();
66+
};
5967

6068
$scope.toggleRow1 = function() {
61-
$scope.gridApi.selection.toggleRowSelection($scope.gridOptions.data[0]);
69+
$scope.gridApi.selection.toggleRowSelection($scope.gridOptions.data[0]);
6270
};
6371

6472
$scope.gridOptions.onRegisterApi = function(gridApi){
@@ -82,6 +90,7 @@ Two examples are provided, the first with rowHeaderSelection and multi-select, t
8290
];
8391

8492
$scope.gridOptions.multiSelect = false;
93+
$scope.gridOptions.modifierKeysToMultiSelect = false;
8594
$scope.gridOptions.noUnselect = true;
8695
$scope.gridOptions.onRegisterApi = function( gridApi ) {
8796
$scope.gridApi = gridApi;
@@ -98,15 +107,18 @@ Two examples are provided, the first with rowHeaderSelection and multi-select, t
98107
</file>
99108
<file name="index.html">
100109
<div ng-controller="MainCtrl">
101-
<button type="button" class="btn btn-success" ng-click="toggleMultiSelect()">Toggle multiSelect</button> <strong>MultiSelect:</strong> <span ng-bind="gridApi.grid.options.multiSelect"></span>
102-
<button type="button" class="btn btn-success" ng-click="toggleRow1()">Toggle Row 0</button> </span>
110+
<button type="button" class="btn btn-success" ng-click="toggleMultiSelect()">Toggle multiSelect</button> <strong>MultiSelect:</strong> <span ng-bind="gridApi.grid.options.multiSelect"></span>
111+
<button type="button" class="btn btn-success" ng-click="toggleRow1()">Toggle Row 0</button>
103112
<br/>
104113
<br/>
105-
<button type="button" class="btn btn-success" ng-disabled="!gridApi.grid.options.multiSelect" ng-click="selectAll()">Select All</button> </span>
106-
<button type="button" class="btn btn-success" ng-click="clearAll()">Clear All</button> </span>
107-
<br>
114+
<button type="button" class="btn btn-success" ng-click="toggleModifierKeysToMultiSelect()">Toggle modifierKeysToMultiSelect</button> <strong>ModifierKeysToMultiSelect:</strong> <span ng-bind="gridApi.grid.options.modifierKeysToMultiSelect"> </span>
115+
<br/>
116+
<br/>
117+
<button type="button" class="btn btn-success" ng-disabled="!gridApi.grid.options.multiSelect" ng-click="selectAll()">Select All</button>
118+
<button type="button" class="btn btn-success" ng-click="clearAll()">Clear All</button>
119+
<br/>
120+
108121
<div ui-grid="gridOptions" ui-grid-selection class="grid"></div>
109-
110122
</div>
111123
<div ng-controller="SecondCtrl">
112124
Single selection grid without rowHeader:

src/features/selection/js/selection.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@
178178
*/
179179
setMultiSelect: function (multiSelect) {
180180
grid.options.multiSelect = multiSelect;
181+
},
182+
/**
183+
* @ngdoc function
184+
* @name setModifierKeysToMultiSelect
185+
* @methodOf ui.grid.selection.api:PublicApi
186+
* @description Sets the current gridOption.modifierKeysToMultiSelect to true or false
187+
* @param {bool} modifierKeysToMultiSelect true to only allow multiple rows when using ctrlKey or shiftKey is used
188+
*/
189+
setModifierKeysToMultiSelect: function (modifierKeysToMultiSelect) {
190+
grid.options.modifierKeysToMultiSelect = modifierKeysToMultiSelect;
181191
}
182192
}
183193
}
@@ -226,6 +236,14 @@
226236
* <br/>Defaults to false
227237
*/
228238
gridOptions.noUnselect = gridOptions.noUnselect === true;
239+
/**
240+
* @ngdoc object
241+
* @name modifierKeysToMultiSelect
242+
* @propertyOf ui.grid.selection.api:GridOptions
243+
* @description Enable multiple row selection only when using the ctrlKey or shiftKey. Requires multiSelect to be true.
244+
* <br/>Defaults to false
245+
*/
246+
gridOptions.modifierKeysToMultiSelect = gridOptions.modifierKeysToMultiSelect === true;
229247
/**
230248
* @ngdoc object
231249
* @name enableRowHeaderSelection
@@ -397,10 +415,12 @@
397415
$scope.selectButtonClick = function(row, evt) {
398416
if (evt.shiftKey) {
399417
uiGridSelectionService.shiftSelect(self, row, self.options.multiSelect);
400-
418+
}
419+
else if (evt.ctrlKey || evt.metaKey) {
420+
uiGridSelectionService.toggleRowSelection(self, row, self.options.multiSelect, self.options.noUnselect);
401421
}
402422
else {
403-
uiGridSelectionService.toggleRowSelection(self, row, self.options.multiSelect, self.options.noUnselect );
423+
uiGridSelectionService.toggleRowSelection(self, row, (self.options.multiSelect && !self.options.modifierKeysToMultiSelect), self.options.noUnselect);
404424
}
405425
};
406426
}
@@ -470,11 +490,13 @@
470490
$elm.on('click', function (evt) {
471491
if (evt.shiftKey) {
472492
uiGridSelectionService.shiftSelect($scope.grid, $scope.row, $scope.grid.options.multiSelect);
473-
474493
}
475-
else {
494+
else if (evt.ctrlKey || evt.metaKey) {
476495
uiGridSelectionService.toggleRowSelection($scope.grid, $scope.row, $scope.grid.options.multiSelect, $scope.grid.options.noUnselect);
477496
}
497+
else {
498+
uiGridSelectionService.toggleRowSelection($scope.grid, $scope.row, ($scope.grid.options.multiSelect && !$scope.grid.options.modifierKeysToMultiSelect), $scope.grid.options.noUnselect);
499+
}
478500
$scope.$apply();
479501
});
480502
}

0 commit comments

Comments
 (0)