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 has been archived by the owner on Jun 7, 2022. It is now read-only.
With regular select2 I use this handy function, that I call on the underlying HTML Select element, and basically it loads the data from the provided URL, makes a new option element, appends it to the select element, then triggers both the plain change and select2 specific events.
But since 🥕 farming is the future...
How can I do this with ng-select2?
If I change the wired [(ngModel)] on an "unused" select2 (meaning user hasn't selected any values yet), then it doesn't show the selection. However, if the user have selected a specific element before, then changed it to something else (either new value or cleared), then if I change the wired model value to the specific element, the correct element is shown as selection. (But neither times does the (valueChanged) event fire.)
I know that regular select2 appends each new selected value to the DOM permanently, so I assume this wrapper does the same thing (and that's why it's able to "show" a previously selected item, because it already exists in the dom), but I haven't been able to observe such DOM manipulation with dev tools or find the underlying select element and observe what happens when I do some clicky clicky. (Then again I'm still new to Angular, so maybe it does some magic on the fly without it reflecting in the DOM.)
The text was updated successfully, but these errors were encountered:
I managed to find a workaround, similair to my original jQuery.
constselect=document.getElementById('myNgSelect2')?.querySelector('select');// select element is first child of the <ng-select2> elementconstoption=document.createElement('option');option.innerText=myOption.text;option.setAttribute('value',myModel.someProperty);/* this selected attirbute makes it kinda jank, because multiple options will have the selected attribute, but ng-select2 seems to handle it well, and always selects the latest added option.I would need more extensive testing, like if this select2 is part of a form, then the correct value gets sent or some random, or maybe first in the DOM?.*/option.setAttribute('selected','selected');select?.appendChild(option);select?.dispatchEvent(newEvent('change'));// needless to say, but this will only work if the view has been initialized :)
With myOption being populated by a server side request. I've put this into my observable.subscribe() method, and it seems the regular select change event is enough to trigger the select2 changing the selected item to this new one.
With regular select2 I use this handy function, that I call on the underlying HTML Select element, and basically it loads the data from the provided URL, makes a new option element, appends it to the select element, then triggers both the plain change and select2 specific events.
But since 🥕 farming is the future...
How can I do this with ng-select2?
If I change the wired [(ngModel)] on an "unused" select2 (meaning user hasn't selected any values yet), then it doesn't show the selection. However, if the user have selected a specific element before, then changed it to something else (either new value or cleared), then if I change the wired model value to the specific element, the correct element is shown as selection. (But neither times does the (valueChanged) event fire.)
I know that regular select2 appends each new selected value to the DOM permanently, so I assume this wrapper does the same thing (and that's why it's able to "show" a previously selected item, because it already exists in the dom), but I haven't been able to observe such DOM manipulation with dev tools or find the underlying select element and observe what happens when I do some clicky clicky. (Then again I'm still new to Angular, so maybe it does some magic on the fly without it reflecting in the DOM.)
The text was updated successfully, but these errors were encountered: