diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 1bd3d1a4f..ad023ccf0 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -45,6 +45,7 @@ uis.controller('uiSelectCtrl', ctrl.clickTriggeredSelect = false; ctrl.$filter = $filter; ctrl.$element = $element; + ctrl.objectUniqueKey = undefined; // Use $injector to check for $animate and store a reference to it ctrl.$animate = (function () { @@ -332,9 +333,13 @@ uis.controller('uiSelectCtrl', return isActive; }; + function _getKeyForComparison(item) { + return ctrl.objectUniqueKey === undefined ? item : item[ctrl.objectUniqueKey]; + } + var _isItemSelected = function (item) { return (ctrl.selected && angular.isArray(ctrl.selected) && - ctrl.selected.filter(function (selection) { return angular.equals(selection, item); }).length > 0); + ctrl.selected.filter(function (selection) { return angular.equals(_getKeyForComparison(selection), _getKeyForComparison(item)); }).length > 0); }; var disabledItems = []; diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 285cfb4ac..ea45a28de 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -127,6 +127,15 @@ uis.directive('uiSelect', } }); + $select.objectUniqueKey = attrs.objectUniqueKey; + attrs.$observe('objectUniqueKey', function() { + if(attrs.objectUniqueKey !== undefined) { + $select.objectUniqueKey = attrs.objectUniqueKey; + } else { + $select.objectUniqueKey = undefined; + } + }); + attrs.$observe('taggingLabel', function() { if(attrs.tagging !== undefined ) { @@ -378,7 +387,7 @@ uis.directive('uiSelect', }; var opened = false; - + scope.calculateDropdownPos = function() { if ($select.open) { dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown'); diff --git a/test/select.spec.js b/test/select.spec.js index 7332c6ec7..e951fa17a 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -2863,6 +2863,25 @@ describe('ui-select tests', function () { expect(el.hasClass('ng-not-empty')).toBeTruthy(); }); + it('should be able to re-select the item with removeselected set to false', function() { + scope.selection.selectedMultiple = [scope.people[4], scope.people[5]]; //Wladimir & Samantha + var el = createUiSelectMultiple({removeSelected : true}); + expect(el.scope().$select.selected.length).toBe(2); + el.find('.ui-select-match-item').first().find('.ui-select-match-close').click(); + expect(el.scope().$select.selected.length).toBe(1); + clickItem(el, 'Wladimir'); + expect(el.scope().$select.selected.length).toBe(2); + }); + + it('should not allow duplicate objects if unique object key is specified', function() { + var el = createUiSelectMultiple({objectUniqueKey: "name"}); + expect(el.scope().$select.selected.length).toBe(0); + clickItem(el, 'Samantha'); + expect(el.scope().$select.selected.length).toBe(1); + clickItem(el, 'Samantha'); + expect(el.scope().$select.selected.length).toBe(1); + }); + it('should be able to re-select the item with removeselected set to false', function () { scope.selection.selectedMultiple = [scope.people[4], scope.people[5]]; //Wladimir & Samantha var el = createUiSelectMultiple({ removeSelected: true });