You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 2, 2019. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 2, 2019. It is now read-only.
Preselect items by remote call in multiple select with object items doesn't work #404
I had a similar problem using $resource. The objects retrieved using $resource are of type Resource and the model object they are compared to is type Object. Otherwise, the two objects are identical. However, the test if (result == value) within checkFnMultiple at line 651 of select.js evaluates to false. I had to swap the model object with its equivalent Resource object to get the option selected.
Ideally, ui-select would allow the user to specify the object property to compare by or provide a comparison function.
I have this same issue and I tested this even without a remote server call, so the server response is not the one causing this issue. In the example Demo Multiple, in Plunker, in the Array of Objects section, by default are two items selected, by doing this in the code:
I have similar problems. The reason seems to be that the control will only add values that are listed in the options. In my use-case i have the profile of a person which might speak several languages. The languages he might have added to his profile previously are not shown in ui-select.
I have similar problems. The reason seems to be that the control will only
add values that are listed in the options. In my use-case i have the
profile of person which might speak several languages. The languages he
might have added to his profile previously are not shown in ui-select.
This behaviour is resulting from the fact that the pre-selected items are checked for equal reference with the provided source items by the checkFnMultiple function (https://github.com/angular-ui/ui-select/blob/master/dist/select.js#L954). That behaviour is nicely demonstrated by @Nomack84 test (Identical objects, but different references). To my current knowledge (first time user) the test is required to keep tidy ship and avoid duplicate values in the list and result-set but the current function fails to handle hard copies of the source items. This #256 should fix this.
I have a somewhat similar problem. I have a link on my form that will populate a remotely populated multi-select control. The item I want to add on the fly will likely not be part of the control's options list (since the list is typically empty as most remotely populated selects will be). Simply setting the value the model that's bound to the select doesn't work (example of the code provided below).
It's pretty disappointing that something like this doesn't work. I have other issues with the remote functionality of the control as well that add to the frustration I guess. Any idea if this will be fixed soon?
Just wanted to play around a bit to see if I could figure out any sort of workaround. I did find one. It's not pretty and I'm not going to use it in the production world, but here it is nonetheless.
Basically, you have to push your own programatically sourced item into the model that's bound to the choice portion of your select control. Once you've done that you can then update the model that's ultimately bound to the control's selected array.
As you can see here the choices are bound to a local solicitorSearchResults array. The control itself is bound to a property on my "data" object called solicitors. I'm passing the control into the locals.solicitorSearch() function for other reasons (cough filtering out currently seleced results from the results array because the control doesn't do it for you cough).
I have this function which is bound to an anchor via ng-click. This function lives on my locals object as well. See the truncated definition of locals below. Once I place the item i want to add into the solicitorSearchResults I was then able to set my solicitors array which would then in turn update the select control. It appears that the act of adding it to the solicitorSearchResults array makes it become "valid" and will then be picked up the select control.
However, I'm sure you what this introduces. Now my multi-select select control; which is populated remotely; has an item in the list of results. When I remove the item I added programatically via the UI and then place focus into the control I'll see that item in the list of available choices......when no remote search has been initiated by the user.
Since this control is remotely sourced on the fly I'm going to have the same issue when I load this from with an existing record. On load I'll have to add all current solicitors to the locals.solicitorSearchResults array and then set the data.solicitors value.
The problem I'm seeing is an inconsistency between the options you need to provide ui-select-choices on initialization vs. when fetch/search is called subsequent times.
To sum it up:
If you want to initialize a multi select with existing selections, then you need to pass those options into ui-select-choices, or else they won't be pre-selected.
However, on subsequent calls to fetch (e.g. when typing), you need to make sure you EXCLUDE any selected options, otherwise it will re-suggest, and let you select duplicates.
I believe the initialization issue (1.) only appeared in a recent release, as I don't remember having to do that in the past. The requirement to exclude options was already existing behavior.
Problem: I would expect to push items onto a watched Choice list but the Match list isn't correctly synchronised in the case where the Match list is populated with objects asynchronously.
Workaround: you must assign the Match list from the Choice list.
Not sure this helps but I solved this issue for my project. It seems like ui.select expects "Resource" type and if you supply "Object", then it fails to display them correctly even though the scope variable has the correct details.
The trick is to ensure that whenever you set in ng-model, it is a Resource type. Lets say that your ng-model is "items" or $scope.items (in controller). Then check the following:
console.log(typeof $scope.items)
If this displays "Object", you have a problem. If it displays "Resource", then you are good.
Activity
richardallen commentedon Nov 18, 2014
I had a similar problem using
$resource. The objects retrieved using$resourceare of typeResourceand the model object they are compared to is typeObject. Otherwise, the two objects are identical. However, the testif (result == value)withincheckFnMultipleat line 651 ofselect.jsevaluates tofalse. I had to swap the model object with its equivalentResourceobject to get the option selected.Ideally, ui-select would allow the user to specify the object property to compare by or provide a comparison function.
richardallen commentedon Nov 18, 2014
PR #256 provides a fix.
alvarezmario commentedon Nov 18, 2014
I have this same issue and I tested this even without a remote server call, so
the server response is not the one causing this issue. In the example Demo Multiple, in Plunker, in the Array of Objects section, by default are two items selected, by doing this in the code:This works fine, but when I change that for this:
The items are no preselected anymore. This is weird, because it's basically the same, so it should work, but it doesn't.
Anyone know why?
yaneq commentedon Nov 19, 2014
I have similar problems. The reason seems to be that the control will only add values that are listed in the options. In my use-case i have the profile of a person which might speak several languages. The languages he might have added to his profile previously are not shown in ui-select.
Here is a demo of the behavior:
http://plnkr.co/edit/swKr14zURxlrvdTdUt7O
alvarezmario commentedon Nov 19, 2014
That's exactly what happens with me
2014-11-19 12:03 GMT-05:00 Jan Koeppen notifications@github.com:
yaneq commentedon Nov 19, 2014
I opened a PR that fixed the problem for me #419
yaneq commentedon Nov 21, 2014
seems to be a duplicate of #192
I solved it now by setting the choices to the model value on page load and it seems to work well.
Adriien-M commentedon Dec 2, 2014
yaneq's fix worked for me... Thanks!
sagar-ranglani commentedon Dec 28, 2014
Facing same problem!
Can someone please explain the solution with an example.
Thank you in advance!
Flightfreak commentedon Jan 15, 2015
This behaviour is resulting from the fact that the pre-selected items are checked for equal reference with the provided source items by the
checkFnMultiplefunction (https://github.com/angular-ui/ui-select/blob/master/dist/select.js#L954). That behaviour is nicely demonstrated by @Nomack84 test (Identical objects, but different references). To my current knowledge (first time user) the test is required to keep tidy ship and avoid duplicate values in the list and result-set but the current function fails to handle hard copies of the source items. This #256 should fix this.ahmer-yasin commentedon Jan 19, 2015
i have one issue when i save data in db it save objectc in array for using ui select
but i retrive the data it will never selected how can i fix it
MikeABentley commentedon Jan 27, 2015
I have a somewhat similar problem. I have a link on my form that will populate a remotely populated multi-select control. The item I want to add on the fly will likely not be part of the control's options list (since the list is typically empty as most remotely populated selects will be). Simply setting the value the model that's bound to the select doesn't work (example of the code provided below).
It's pretty disappointing that something like this doesn't work. I have other issues with the remote functionality of the control as well that add to the frustration I guess. Any idea if this will be fixed soon?
$scope.data.solicitors.push({ id: $scope.data.current_user_contact_id, name: $scope.data.current_user_contact_name });
You would think that after updating the value of the model bound to the select control would then update the selected array in the control as well.
MikeABentley commentedon Jan 28, 2015
Just wanted to play around a bit to see if I could figure out any sort of workaround. I did find one. It's not pretty and I'm not going to use it in the production world, but here it is nonetheless.
Basically, you have to push your own programatically sourced item into the model that's bound to the choice portion of your select control. Once you've done that you can then update the model that's ultimately bound to the control's selected array.
As you can see here the choices are bound to a local solicitorSearchResults array. The control itself is bound to a property on my "data" object called solicitors. I'm passing the control into the locals.solicitorSearch() function for other reasons (cough filtering out currently seleced results from the results array because the control doesn't do it for you cough).
I have this function which is bound to an anchor via ng-click. This function lives on my locals object as well. See the truncated definition of locals below. Once I place the item i want to add into the solicitorSearchResults I was then able to set my solicitors array which would then in turn update the select control. It appears that the act of adding it to the solicitorSearchResults array makes it become "valid" and will then be picked up the select control.
However, I'm sure you what this introduces. Now my multi-select select control; which is populated remotely; has an item in the list of results. When I remove the item I added programatically via the UI and then place focus into the control I'll see that item in the list of available choices......when no remote search has been initiated by the user.
Since this control is remotely sourced on the fly I'm going to have the same issue when I load this from with an existing record. On load I'll have to add all current solicitors to the locals.solicitorSearchResults array and then set the data.solicitors value.
sporkd commentedon Jan 30, 2015
The problem I'm seeing is an inconsistency between the options you need to provide
ui-select-choiceson initialization vs. when fetch/search is called subsequent times.To sum it up:
ui-select-choices, or else they won't be pre-selected.I believe the initialization issue (1.) only appeared in a recent release, as I don't remember having to do that in the past. The requirement to exclude options was already existing behavior.
jukkasi commentedon Jan 31, 2015
@MBentley1976 I think I'm facing similar problem, described here #256
Fix model binding error. Props to @b3n0n. Fixes #531, #404, #256.
v.0.9.8. Fixes #256, #404, #5321, #3, #456, #657, #597, #534, (maybe …
brazorf commentedon Apr 20, 2015
I've just upgraded to 0.11.2, and still this doesn't seem to be fixed
olso commentedon Apr 21, 2015
0.11.2 here...
Still facing problems with duplicates when having ngModel (multiple) set before choices are loaded as @sporkd explained.
For now, I "fixed" it by $watching when choices are populated and then I set ngModel.
However, Angular digest cycle is a whole another problem with this**...**
olso commentedon Apr 22, 2015
@brazorf Have you fixed it or what did you do?
Zach-Barnes-Volusion commentedon May 5, 2015
+1 Still broken in 0.11.2
toddb commentedon Jun 28, 2015
+1 broken - I am pretty sure this is the same issue I found. I am using Array of Objects too.
See http://plnkr.co/edit/PJdDL58wCx5XiKsUCOw3 for demonstration of problem and solution/workaround.
Problem: I would expect to push items onto a watched Choice list but the Match list isn't correctly synchronised in the case where the Match list is populated with objects asynchronously.
Workaround: you must assign the Match list from the Choice list.
bademux commentedon Aug 11, 2015
+1
JonnyBGod commentedon Sep 15, 2015
+1
codegeek1001 commentedon Nov 4, 2015
Not sure this helps but I solved this issue for my project. It seems like ui.select expects "Resource" type and if you supply "Object", then it fails to display them correctly even though the scope variable has the correct details.
The trick is to ensure that whenever you set in ng-model, it is a Resource type. Lets say that your ng-model is "items" or $scope.items (in controller). Then check the following:
If this displays "Object", you have a problem. If it displays "Resource", then you are good.
gentisaliu commentedon Jun 20, 2017
+1!
malkitbhasin commentedon Oct 11, 2017
+1