I was playing around with my trusty multi-select dropdown earlier today and a colleague of mine pointed out that changing the selected items outside of the directive would not actually deselect previously selected items. This was a curious bug since I knew I had a watcher on the ngModel selections.
Setting the selected items on first load always worked without a hitch. But I found that I had put in some conditional logic that was not firing the “mark checked” code that I had in place. The “mark checked” code would simply iterate over all of the internal items and compare their values to ngModel, whether it was simple scalar or complex object, and set the checked property. The comparison itself wasn’t comparing apples to apples, though, and nothing was being checked. Additionally, clearing out ngModel did nothing. Finally, since ngModel would change inside of the directive itself when calling modelCtrl.$setViewValue, I wanted to avoid the looping over the items and ngModel array unnecessarily.
I won’t post a ton of code here showing the changes, but suffice it to say there is a bit set when the directive itself has triggered a change to the ngModel. When this bit is set, the iterative code that sets the checkmarks is not called when the ngModel watcher is triggered. Conversely, if an external process updates ngModel, the code will fire to compare the new selection vs. the available items and mark them as checked or unchecked appropriately.
The updated demo shows how randomly setting the selected items or clearing the selecting items stored in ngModel will now be properly handled by the directive.
The full source can be seen on Github.
There’s a plunk if you prefer Plunker and pen if your prefer Codepen.
order by is not working in multiselect
It should work.
The basic premise is that you have options defined with key/value:
By default, the item, has top-level properties only of checked, key, and value. However, the true underlying model is attached as an object named model. Therefore, if, for example, I were to change the demo options (vm.options1) to add a random number to the underlying model like so:
Then the property is accessible via the “order-label” by specifying like this. order-label=”model.randnum”. Here’s how that would look in the mark-up:
Hope that helps ..
Hi Stephen and congrats.
How can i add my own text instead of “Prop10”, “Prop9” etc?
Thanks in advance.
P.S. I know nothing about coding…
My mistake, forgive me. I would like to be shown my own values and not numbers like 1, 2, 3, 4 etc or “Prop1”. Thank you.
The general paradigm for loading a dropdown in Angular is to use “options” (or ng-options) to create, effectively, a dictionary of key value pairs to display in the dropdown.
Within the mark-up of the demo(s), then, you’ll see this a lot:
Here, any key/value combination can be extracted from your list of objects. If we look at the source code for how vm.options1 is populated, it looks like this:
If you wanted to use your own properties, it might look something like this:
And if we wanted to display those values, it would look like this in the mark-up:
Finally, we’d get something like this displayed:
Hope that helps..
This is solved. Thank you very much for your time, your effort and your quick answer. Keep up!
Hi,
Is it possible to add lazy loading option for long lists?