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

Commit f953cc3

Browse files
committed
Merge pull request #1110 from cdjackson/limit_multi_select
Limit the maximum number of selections allowed in multiple mode
2 parents bb96d7c + e28ecec commit f953cc3

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

src/uiSelectDirective.js

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ uis.directive('uiSelect',
4444
$select.onSelectCallback = $parse(attrs.onSelect);
4545
$select.onRemoveCallback = $parse(attrs.onRemove);
4646

47+
//Limit the number of selections allowed
48+
$select.limit = (angular.isDefined(attrs.limit)) ? parseInt(attrs.limit, 10) : undefined;
49+
4750
//Set reference to ngModel from uiSelectCtrl
4851
$select.ngModel = ngModel;
4952

src/uiSelectMultipleDirective.js

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
155155
};
156156

157157
scope.$on('uis:select', function (event, item) {
158+
if($select.selected.length >= $select.limit) {
159+
return;
160+
}
158161
$select.selected.push(item);
159162
$selectMultiple.updateModel();
160163
});

test/select.spec.js

+53-20
Original file line numberDiff line numberDiff line change
@@ -1754,34 +1754,67 @@ describe('ui-select tests', function() {
17541754

17551755
});
17561756

1757-
it('should watch changes for $select.selected and update formatted value correctly', function () {
1757+
it('should watch changes for $select.selected and update formatted value correctly', function () {
17581758

1759-
scope.selection.selectedMultiple = ['[email protected]', '[email protected]'];
1759+
scope.selection.selectedMultiple = ['[email protected]', '[email protected]'];
17601760

1761-
var el = compileTemplate(
1762-
'<ui-select multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
1763-
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
1764-
<ui-select-choices repeat="person.email as person in people | filter: $select.search"> \
1765-
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1766-
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1767-
</ui-select-choices> \
1768-
</ui-select> \
1769-
'
1770-
);
1761+
var el = compileTemplate(
1762+
'<ui-select multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
1763+
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
1764+
<ui-select-choices repeat="person.email as person in people | filter: $select.search"> \
1765+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1766+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1767+
</ui-select-choices> \
1768+
</ui-select> \
1769+
'
1770+
);
17711771

1772-
var el2 = compileTemplate('<span class="resultDiv" ng-bind="selection.selectedMultiple"></span>');
1772+
var el2 = compileTemplate('<span class="resultDiv" ng-bind="selection.selectedMultiple"></span>');
17731773

1774-
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1775-
.toBe("Wladimir <[email protected]>Samantha <[email protected]>");
1774+
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1775+
.toBe("Wladimir <[email protected]>Samantha <[email protected]>");
17761776

1777-
clickItem(el, 'Nicole');
1777+
clickItem(el, 'Nicole');
17781778

1779-
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1780-
.toBe("Wladimir <[email protected]>Samantha <[email protected]>Nicole <[email protected]>");
1779+
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1780+
.toBe("Wladimir <[email protected]>Samantha <[email protected]>Nicole <[email protected]>");
17811781

1782-
expect(scope.selection.selectedMultiple.length).toBe(3);
1782+
expect(scope.selection.selectedMultiple.length).toBe(3);
17831783

1784-
});
1784+
});
1785+
1786+
it('should ensure the multiple selection limit is respected', function () {
1787+
1788+
scope.selection.selectedMultiple = ['[email protected]'];
1789+
1790+
var el = compileTemplate(
1791+
'<ui-select multiple limit="2" ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
1792+
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
1793+
<ui-select-choices repeat="person.email as person in people | filter: $select.search"> \
1794+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1795+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1796+
</ui-select-choices> \
1797+
</ui-select> \
1798+
'
1799+
);
1800+
1801+
var el2 = compileTemplate('<span class="resultDiv" ng-bind="selection.selectedMultiple"></span>');
1802+
1803+
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1804+
.toBe("Wladimir <[email protected]>");
1805+
1806+
clickItem(el, 'Samantha');
1807+
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1808+
.toBe("Wladimir <[email protected]>Samantha <[email protected]>");
1809+
1810+
clickItem(el, 'Nicole');
1811+
1812+
expect(el.find('.ui-select-match-item [uis-transclude-append]:not(.ng-hide)').text())
1813+
.toBe("Wladimir <[email protected]>Samantha <[email protected]>");
1814+
1815+
expect(scope.selection.selectedMultiple.length).toBe(2);
1816+
1817+
});
17851818

17861819
it('should change viewvalue only once when updating modelvalue', function () {
17871820

0 commit comments

Comments
 (0)