diff --git a/docs/assets/demo.js b/docs/assets/demo.js index f9999d4a7..9d6702c4c 100644 --- a/docs/assets/demo.js +++ b/docs/assets/demo.js @@ -150,6 +150,8 @@ app.controller('DemoCtrl', function ($scope, $http, $timeout, $interval) { vm.person.selectedValue = vm.peopleObj[3]; vm.person.selectedSingle = 'Samantha'; vm.person.selectedSingleKey = '5'; + // To run the demos with a preselected person object, uncomment the line below. + //vm.person.selected = vm.person.selectedValue; vm.people = [ { name: 'Adam', email: 'adam@email.com', age: 12, country: 'United States' }, diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 09721ef6a..1a5afec85 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -235,7 +235,7 @@ uis.controller('uiSelectCtrl', data = data || ctrl.parserResult.source($scope); var selectedItems = ctrl.selected; //TODO should implement for single mode removeSelected - if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.removeSelected) { + if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.multiple || !ctrl.removeSelected) { ctrl.setItemsFn(data); }else{ if ( data !== undefined && data !== null ) { diff --git a/test/select.spec.js b/test/select.spec.js index e502c3995..fbfeb8a93 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -1427,7 +1427,7 @@ describe('ui-select tests', function() { expect($(el).scope().$select.selected).toEqual(['idontexist']); }); - it('should remove a choice when remove-selected is not given (default is true)', function () { + it('should remove a choice when multiple and remove-selected is not given (default is true)', function () { var el = compileTemplate( ' \ @@ -1453,6 +1453,31 @@ describe('ui-select tests', function() { }); }); + it('should not remove a pre-selected choice when not multiple and remove-selected is not given (default is true)', function () { + scope.selection.selected = scope.people[5]; // Samantha + + var el = compileTemplate( + ' \ + {{$select.selected.name}} \ + \ +
\ +
\ +
\ +
' + ); + + expect(getMatchLabel(el)).toEqual("Samantha"); + openDropdown(el); + + var choicesEls = $(el).find('.ui-select-choices-row'); + expect(choicesEls.length).toEqual(8); + + ['Adam', 'Amalie', 'Estefanía', 'Adrian', 'Wladimir', 'Samantha', 'Nicole', 'Natasha'].forEach(function (name, index) { + expect($(choicesEls[index]).hasClass('disabled')).toBeFalsy(); + expect($(choicesEls[index]).find('.person-name').text()).toEqual(name); + }); + }); + it('should disable a choice instead of removing it when remove-selected is false', function () { var el = compileTemplate(