Open triawarman opened 11 months ago
I think a feature like this would be very nice. I had to write a bunch of NiceSelect.prototype.update
lines in my code to handle cases where the source value would change in the original select box, such as when the user chooses to load from a preexisting configuration file of data choices.
I found a workarround: get all the option of orignal element and set the attribute selected accordingly and then call the update() function
let originalSelectElement = document.getElementById('originalSelectElementId');
originalSelectElement.value = valueToSelect; /* the value to update */
/* loop the options and set selected or remove it */
for (let index = 0; index < originalSelectElement.options.length; index++) {
let option = originalSelectElement.options[index];
if (option.value == valueToSelect) {
option.selected = true;
option.setAttribute("selected", "selected");
} else {
option.selected = false;
option.removeAttribute("selected");
}
}
/* call update to nice select element */
niceSelectElement.update();
There are conditions when changing control value are not based on user interaction, but based on automation code, currently select2 has no methode that can sync the select2 selected value with source value when the source value is changed.
i hope you guys make the synchronization method/function.