Skip to content

support selecting individual values #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 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
9 changes: 5 additions & 4 deletions angular-bootstrap-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ angular.module("ui.multiselect", ["multiselect.tpl.html"])
itemName : match[3],
source : $parse(match[4]),
viewMapper : $parse(match[2] || match[1]),
modelMapper: $parse(match[1])
modelMapper: $parse(match[2] ? match[1] : match[3])
};
}
};
Expand Down Expand Up @@ -91,6 +91,7 @@ angular.module("ui.multiselect", ["multiselect.tpl.html"])
}, function(newVal) {
if(angular.isDefined(newVal)) {
parseModel();
markChecked(modelCtrl.$modelValue);
}
}, true);

Expand All @@ -101,7 +102,7 @@ angular.module("ui.multiselect", ["multiselect.tpl.html"])
//when directive initialize, newVal usually undefined. Also, if model value already set in the controller
//for preselected list then we need to mark checked in our scope item. But we don't want to do this every time
//model changes. We need to do this only if it is done outside directive scope, from controller, for example.
if(angular.isDefined(newVal)) {
if(newVal !== oldVal) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the model is set to undefined, you should still update the selection. this still handles the initialization case when both newVal and oldVal are undefined.

markChecked(newVal);
scope.$eval(changeHandler);
}
Expand All @@ -120,7 +121,7 @@ angular.module("ui.multiselect", ["multiselect.tpl.html"])
local[parsedResult.itemName] = model[i];
scope.items.push({
label : parsedResult.viewMapper(local),
model : model[i],
model : parsedResult.modelMapper(local),
checked: false
});
}
Expand Down Expand Up @@ -215,7 +216,7 @@ angular.module("ui.multiselect", ["multiselect.tpl.html"])
}

function markChecked(newVal) {
if(!angular.isArray(newVal)) {
if(angular.isDefined(newVal) && !angular.isArray(newVal)) {
angular.forEach(scope.items, function(item) {
item.checked = false;
if(compareByKey === undefined && angular.equals(item.model, newVal)) {
Expand Down
5 changes: 3 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ angular.module("angular-bootstrap-multiselect-test", ["ui.multiselect"]).control
name: "Peaches",
bla: "blubber"
}
]
],
[]
];

$scope.selectOptions = [
Expand Down Expand Up @@ -88,4 +89,4 @@ angular.module("angular-bootstrap-multiselect-test", ["ui.multiselect"]).control
"ThisIsATestWithVeryLongEntries44444",
"ThisIsATestWithVeryLongEntries55555",
];
});
});
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-bootstrap-multiselect",
"version": "0.4.0",
"version": "0.5.0",
"authors": [
"Sebastian Hammerl, Getslash GmbH",
"Amitava Saha"
Expand Down
17 changes: 15 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ <h3>Multi select pre-selection compare by id</h3>
&gt;&lt;/multiselect&gt</pre>
<br>

<h3>Multi select value with objects</h3>
<multiselect ng-model="multiselectModel[5]" options="c.id as c.name for c in selectOptionsObjects" data-multiple="true"></multiselect>
<br><br>
Model: {{multiselectModel[5]}}
<br><br>
<pre>&lt;multiselect
ng-model="multiselectModel[5]"
options="c.id as c.name for c in selectOptionsObjects"
data-multiple="true"
&gt;&lt;/multiselect&gt</pre>
<br>

<h3>app.js:</h3>
<pre>
angular.module("angular-bootstrap-multiselect-test", ["ui.multiselect"]).controller("index", function($scope) {
Expand Down Expand Up @@ -223,7 +235,8 @@ <h3>app.js:</h3>
name: "Peaches",
bla: "blubber"
}
]
],
[]
];

$scope.selectOptions = [
Expand Down Expand Up @@ -272,4 +285,4 @@ <h3>app.js:</h3>
</div>
</div>
</body>
</html>
</html>