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

Fix multiselect population (Issue #333) #414

Closed
Closed
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
Empty file modified dist/select.js
100755 → 100644
Empty file.
Empty file modified dist/select.min.js
100755 → 100644
Empty file.
18 changes: 18 additions & 0 deletions examples/demo-multi-select.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ <h3>Array of objects (with groupBy)</h3>
</ui-select>
<p>Selected: {{multipleDemo.selectedPeopleWithGroupBy}}</p>

<hr>
<h3>Populate multi select</h3>
<p>
<button ng-click="populate([{name: 'Adam', email: '[email protected]', age: 12, country: 'United States'}])">Populate 'Adam'</button>
<button ng-click="populate([{name: 'Adam', email: '[email protected]', age: 12, country: 'United States'}, {name: 'Estefanía', email: '[email protected]', age: 21, country: 'Argentina'}])">Populate 'Adam' and 'Estefanía'</button>
</p>
<ui-select multiple ng-model="multipleDemo.populateSelection" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
<ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="person.name | highlight: $select.search"></div>
<small>
email: {{person.email}}
age: <span ng-bind-html="''+person.age | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
<p>Selected: {{multipleDemo.populateSelection}}</p>

<div style="height:500px"></div>

</body>
Expand Down
12 changes: 12 additions & 0 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ app.controller('DemoCtrl', function($scope, $http, $timeout) {
});
};

$scope.populate = function(persons) {
if (!persons) {
return;
}
$scope.multipleDemo.populateSelection = [];
$timeout(function() {
angular.forEach(persons, function(value) {
$scope.multipleDemo.populateSelection.push(value);
});
});
};

$scope.country = {};
$scope.countries = [ // Taken from https://gist.github.com/unceus/6501985
{name: 'Afghanistan', code: 'AF'},
Expand Down
33 changes: 30 additions & 3 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,18 +658,45 @@

//From model --> view
ngModel.$formatters.unshift(function (inputValue) {
var data = $select.parserResult.source (scope, { $select : {search:''}}), //Overwrite $search
var data = $select.parserResult.source (scope, { $select : {search:''}}), //Overwrite $search
locals = {},
result;
if (data){
if ($select.multiple){
var resultMultiple = [];
var resultMultiple = [],
parseValue,
unshiftResult;
var checkFnMultiple = function(list, value){
if (!list || !list.length) return;
for (var p = list.length - 1; p >= 0; p--) {
unshiftResult = false;
locals[$select.parserResult.itemName] = list[p];
result = $select.parserResult.modelMapper(scope, locals);
if (result == value){
locals[$select.parserResult.itemName] = value;
parseValue = $select.parserResult.modelMapper(scope, locals);

if (parseValue) {
if (angular.isObject(result)) {
if (angular.equals(result, parseValue)) {
unshiftResult = true;
}
} else {
if (result == parseValue) {
unshiftResult = true;
}
}
} else {
if (angular.isObject(result)) {
if (angular.equals(result, value)) {
unshiftResult = true;
}
} else {
if (result == value) {
unshiftResult = true;
}
}
}
if (unshiftResult) {
resultMultiple.unshift(list[p]);
return true;
}
Expand Down