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

Commit 04eb3f6

Browse files
committed
Merge pull request #1519 from user378230/fix-data-attrs
fix(uiSelect) support data-multiple attribute
2 parents 2cc07dc + 8111ad1 commit 04eb3f6

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/uiSelectMatchDirective.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ uis.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {
88
// Needed so the uiSelect can detect the transcluded content
99
tElement.addClass('ui-select-match');
1010

11+
var parent = tElement.parent();
1112
// Gets theme attribute from parent (ui-select)
12-
var theme = tElement.parent().attr('theme') || uiSelectConfig.theme;
13-
var multi = tElement.parent().attr('multiple');
14-
return theme + (multi ? '/match-multiple.tpl.html' : '/match.tpl.html');
13+
var theme = getAttribute(parent, 'theme') || uiSelectConfig.theme;
14+
var multi = angular.isDefined(getAttribute(parent, 'multiple'));
15+
16+
return theme + (multi ? '/match-multiple.tpl.html' : '/match.tpl.html');
1517
},
1618
link: function(scope, element, attrs, $select) {
1719
$select.lockChoiceExpression = attrs.uiLockChoice;
@@ -32,4 +34,15 @@ uis.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {
3234

3335
}
3436
};
37+
38+
function getAttribute(elem, attribute) {
39+
if (elem[0].hasAttribute(attribute))
40+
return elem.attr(attribute);
41+
42+
if (elem[0].hasAttribute('data-' + attribute))
43+
return elem.attr('data-' + attribute);
44+
45+
if (elem[0].hasAttribute('x-' + attribute))
46+
return elem.attr('x-' + attribute);
47+
}
3548
}]);

test/select.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,44 @@ describe('ui-select tests', function() {
16031603
expect(el.find('.ui-select-match-item').length).toBe(0);
16041604
});
16051605

1606+
it('should render intial state with data-multiple attribute', function () {
1607+
// ensure match template has been loaded by having more than one selection
1608+
scope.selection.selectedMultiple = [scope.people[0], scope.people[1]];
1609+
1610+
var el = compileTemplate(
1611+
'<ui-select data-multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
1612+
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
1613+
<ui-select-choices repeat="person in people | filter: $select.search"> \
1614+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1615+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1616+
</ui-select-choices> \
1617+
</ui-select>'
1618+
);
1619+
1620+
expect(el).toHaveClass('ui-select-multiple');
1621+
expect(el.scope().$select.selected.length).toBe(2);
1622+
expect(el.find('.ui-select-match-item').length).toBe(2);
1623+
});
1624+
1625+
it('should render intial state with x-multiple attribute', function () {
1626+
// ensure match template has been loaded by having more than one selection
1627+
scope.selection.selectedMultiple = [scope.people[0], scope.people[1]];
1628+
1629+
var el = compileTemplate(
1630+
'<ui-select x-multiple ng-model="selection.selectedMultiple" theme="bootstrap" style="width: 800px;"> \
1631+
<ui-select-match placeholder="Pick one...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match> \
1632+
<ui-select-choices repeat="person in people | filter: $select.search"> \
1633+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1634+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
1635+
</ui-select-choices> \
1636+
</ui-select>'
1637+
);
1638+
1639+
expect(el).toHaveClass('ui-select-multiple');
1640+
expect(el.scope().$select.selected.length).toBe(2);
1641+
expect(el.find('.ui-select-match-item').length).toBe(2);
1642+
});
1643+
16061644
it('should set model as an empty array if ngModel isnt defined after an item is selected', function () {
16071645

16081646
// scope.selection.selectedMultiple = [];

0 commit comments

Comments
 (0)