Skip to content

Commit 98ab013

Browse files
committed
Merge pull request angular-ui#262 from vitosamson/master
Add callback for removal of items from multi-select
2 parents 97d0243 + f353e3b commit 98ab013

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

examples/demo-multi-select.html

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ <h3>Array of objects</h3>
134134
</ui-select>
135135
<p>Selected: {{multipleDemo.selectedPeople}}</p>
136136

137+
<hr>
138+
<h3>Deselect callback with single property binding</h3>
139+
<ui-select multiple ng-model="multipleDemo.deSelectedPeople" on-remove="removed($item, $model)" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
140+
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
141+
<ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
142+
<div ng-bind-html="person.name | highlight: $select.search"></div>
143+
<small>
144+
email: {{person.email}}
145+
age: <span ng-bind-html="''+person.age | highlight: $select.search"></span>
146+
</small>
147+
</ui-select-choices>
148+
</ui-select>
149+
<p>Last removed item: {{lastRemoved.item}}</p>
150+
<p>Last removed model: {{lastRemoved.model}}</p>
151+
137152
<hr>
138153
<h3>Array of objects with single property binding</h3>
139154
<ui-select multiple ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">

examples/demo.js

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ app.controller('DemoCtrl', function($scope, $http, $timeout) {
9999
$scope.eventResult = {item: item, model: model};
100100
};
101101

102+
$scope.removed = function (item, model) {
103+
$scope.lastRemoved = {
104+
item: item,
105+
model: model
106+
};
107+
};
108+
102109
$scope.person = {};
103110
$scope.people = [
104111
{ name: 'Adam', email: 'adam@email.com', age: 12, country: 'United States' },

src/select.js

+10
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,18 @@
360360

361361
// Remove item from multiple select
362362
ctrl.removeChoice = function(index){
363+
var removedChoice = ctrl.selected[index];
364+
var locals = {};
365+
locals[ctrl.parserResult.itemName] = removedChoice;
366+
363367
ctrl.selected.splice(index, 1);
364368
ctrl.activeMatchIndex = -1;
365369
ctrl.sizeSearchInput();
370+
371+
ctrl.onRemoveCallback($scope, {
372+
$item: removedChoice,
373+
$model: ctrl.parserResult.modelMapper($scope, locals)
374+
});
366375
};
367376

368377
ctrl.getPlaceholder = function(){
@@ -575,6 +584,7 @@
575584
$select.multiple = angular.isDefined(attrs.multiple);
576585

577586
$select.onSelectCallback = $parse(attrs.onSelect);
587+
$select.onRemoveCallback = $parse(attrs.onRemove);
578588

579589
//From view --> model
580590
ngModel.$parsers.unshift(function (inputValue) {

test/select.spec.js

+57
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,63 @@ describe('ui-select tests', function() {
728728

729729
});
730730

731+
it('should invoke remove callback on remove', function () {
732+
733+
scope.onRemoveFn = function ($item, $model, $label) {
734+
scope.$item = $item;
735+
scope.$model = $model;
736+
};
737+
738+
var el = compileTemplate(
739+
'<ui-select multiple on-remove="onRemoveFn($item, $model)" ng-model="selection.selected"> \
740+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
741+
<ui-select-choices repeat="person.name as person in people | filter: $select.search"> \
742+
<div ng-bind-html="person.name" | highlight: $select.search"></div> \
743+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
744+
</ui-select-choices> \
745+
</ui-select>'
746+
);
747+
748+
expect(scope.$item).toBeFalsy();
749+
expect(scope.$model).toBeFalsy();
750+
751+
clickItem(el, 'Samantha');
752+
clickItem(el, 'Adrian');
753+
el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
754+
755+
expect(scope.$item).toBe(scope.people[5]);
756+
expect(scope.$model).toBe('Samantha');
757+
758+
});
759+
760+
it('should set $item & $model correctly when invoking callback on remove and no single prop. binding', function () {
761+
762+
scope.onRemoveFn = function ($item, $model, $label) {
763+
scope.$item = $item;
764+
scope.$model = $model;
765+
};
766+
767+
var el = compileTemplate(
768+
'<ui-select multiple on-remove="onRemoveFn($item, $model)" ng-model="selection.selected"> \
769+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
770+
<ui-select-choices repeat="person in people | filter: $select.search"> \
771+
<div ng-bind-html="person.name" | highlight: $select.search"></div> \
772+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
773+
</ui-select-choices> \
774+
</ui-select>'
775+
);
776+
777+
expect(scope.$item).toBeFalsy();
778+
expect(scope.$model).toBeFalsy();
779+
780+
clickItem(el, 'Samantha');
781+
clickItem(el, 'Adrian');
782+
el.find('.ui-select-match-item').first().find('.ui-select-match-close').click();
783+
784+
expect(scope.$item).toBe(scope.people[5]);
785+
expect(scope.$model).toBe(scope.$item);
786+
});
787+
731788
it('should append/transclude content (with correct scope) that users add at <match> tag', function () {
732789

733790
var el = compileTemplate(

0 commit comments

Comments
 (0)