-
Notifications
You must be signed in to change notification settings - Fork 62
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
[Feature Request] Sync nice-select2 value with source control value, when the source value changed #78
Comments
I think a feature like this would be very nice. I had to write a bunch of |
I found a workarround: get all the option of orignal element and set the attribute selected accordingly and then call the update() function
|
this supports multi-selects as well :) const binding = NiceSelect.bind(select)
select.addEventListener("change", () => {
const selected = Array.from(select.selectedOptions);
for (const option of Array.from(select.options)) {
if (selected.includes(option)) {
option.selected = true;
option.setAttribute("selected", "selected");
} else {
option.selected = false;
option.removeAttribute("selected");
}
}
binding.update();
}); However, this does not work completely. This is create for a one-time thing, but afterwards the manual usage via the mouse is broken in nice-select2 as it works on an old state (not sure why). I've changed my code that updates the select programmatically, to fake a click on the NiceSelect option instead. Pretty hacky. const found = niceSelectInstances.find((ns) => ns.el === element);
const options = Array.from(found.dropdown.querySelectorAll(".option")); // class from the internals
const option = options.find(
(o) => o.dataset.value === selectedOptionsValue // dataset usage from nice-selects internals
);
// fake click to have the nice select update properly
option.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
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.
The text was updated successfully, but these errors were encountered: