Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 1ea8155

Browse files
committed
feat(templates): Support disabled options in select menu.
1 parent 59ddf64 commit 1ea8155

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed

src/bootstrap/choices.tpl.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<li class="ui-select-choices-group">
55
<div class="divider" ng-show="$select.isGrouped && $index > 0"></div>
66
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label dropdown-header">{{$group}}</div>
7-
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this)}">
7+
{{item}}
8+
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}">
89
<a href="javascript:void(0)" class="ui-select-choices-row-inner"></a>
910
</div>
1011
</li>

src/select.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@
122122
background-color: #428bca;
123123
}
124124

125+
.ui-select-bootstrap .ui-select-choices-row.disabled>a,
126+
.ui-select-bootstrap .ui-select-choices-row.active.disabled>a {
127+
color: #777;
128+
cursor: not-allowed;
129+
background-color: #fff;
130+
}
131+
125132
/* fix hide/show angular animation */
126133
.ui-select-match.ng-hide-add,
127134
.ui-select-search.ng-hide-add {

src/select.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,35 @@
229229
return ctrl.items.indexOf(itemScope[ctrl.itemProperty]) === ctrl.activeIndex;
230230
};
231231

232+
ctrl.isDisabled = function(itemScope) {
233+
var itemIndex = ctrl.items.indexOf(itemScope[ctrl.itemProperty]);
234+
var isDisabled = false;
235+
var item;
236+
237+
if (itemIndex >= 0) {
238+
item = ctrl.items[itemIndex];
239+
isDisabled = item.disabled;
240+
}
241+
242+
return isDisabled;
243+
};
244+
232245
// When the user clicks on an item inside the dropdown
233246
ctrl.select = function(item) {
234247

235-
var locals = {};
236-
locals[ctrl.parserResult.itemName] = item;
248+
if (!item.disabled) {
249+
var locals = {};
250+
locals[ctrl.parserResult.itemName] = item;
237251

238-
ctrl.onSelectCallback($scope, {
239-
$item: item,
240-
$model: ctrl.parserResult.modelMapper($scope, locals)
241-
});
252+
ctrl.onSelectCallback($scope, {
253+
$item: item,
254+
$model: ctrl.parserResult.modelMapper($scope, locals)
255+
});
242256

243-
ctrl.selected = item;
244-
ctrl.close();
245-
// Using a watch instead of $scope.ngModel.$setViewValue(item)
257+
ctrl.selected = item;
258+
ctrl.close();
259+
// Using a watch instead of $scope.ngModel.$setViewValue(item)
260+
}
246261
};
247262

248263
// Closes the dropdown

src/select2/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<li class="ui-select-choices-group" ng-class="{'select2-result-with-children': $select.isGrouped}">
33
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label select2-result-label">{{$group}}</div>
44
<ul ng-class="{'select2-result-sub': $select.isGrouped, 'select2-result-single': !$select.isGrouped}">
5-
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.isActive(this)}">
5+
<li class="ui-select-choices-row" ng-class="{'select2-highlighted': $select.isActive(this), 'select2-disabled': $select.isDisabled(this)}">
66
<div class="select2-result-label ui-select-choices-row-inner"></div>
77
</li>
88
</ul>

src/selectize/choices.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="ui-select-choices-content selectize-dropdown-content">
33
<div class="ui-select-choices-group optgroup">
44
<div ng-show="$select.isGrouped" class="ui-select-choices-group-label optgroup-header">{{$group}}</div>
5-
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this)}">
5+
<div class="ui-select-choices-row" ng-class="{active: $select.isActive(this), disabled: $select.isDisabled(this)}">
66
<div class="option ui-select-choices-row-inner" data-selectable></div>
77
</div>
88
</div>

test/select.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,49 @@ describe('ui-select tests', function() {
206206
expect(getMatchLabel(el)).toEqual('false');
207207
});
208208

209+
describe('disabled options', function() {
210+
function createUiSelect() {
211+
return compileTemplate(
212+
'<ui-select ng-model="selection.selected"> \
213+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
214+
<ui-select-choices repeat="person in people | filter: $select.search"> \
215+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
216+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
217+
</ui-select-choices> \
218+
</ui-select>'
219+
);
220+
}
221+
222+
beforeEach(function() {
223+
scope._people = angular.copy(scope.people);
224+
scope.people = scope.people.map(function (person) {
225+
if (person.name == 'Wladimir') {
226+
person.disabled = true;
227+
}
228+
return person;
229+
});
230+
231+
this.el = createUiSelect();
232+
});
233+
234+
afterEach(function() {
235+
scope.people = angular.copy(scope._people);
236+
});
237+
238+
it('should not allow disabled options to be selected', function() {
239+
clickItem(this.el, 'Wladimir');
240+
241+
expect(getMatchLabel(this.el)).not.toEqual('Wladimir');
242+
});
243+
244+
it('should set a disabled class on the option', function() {
245+
var option = $(this.el).find('.ui-select-choices-row div:contains("Wladimir")');
246+
var container = option.closest('.ui-select-choices-row');
247+
248+
expect(container.hasClass('disabled')).toBeTruthy();
249+
});
250+
});
251+
209252
describe('choices group', function() {
210253
function getGroupLabel(item) {
211254
return item.parent('.ui-select-choices-group').find('.ui-select-choices-group-label');

0 commit comments

Comments
 (0)