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

Issue 1820 #1830

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 = [];
Expand Down
11 changes: 10 additions & 1 deletion src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -378,7 +387,7 @@ uis.directive('uiSelect',
};

var opened = false;

scope.calculateDropdownPos = function() {
if ($select.open) {
dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown');
Expand Down
19 changes: 19 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down