Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Changing [value] for multiple select only adds items to selection (does not remove any) #141

Open
mactyr opened this issue Feb 27, 2019 · 3 comments · May be fixed by #150
Open

Changing [value] for multiple select only adds items to selection (does not remove any) #141

mactyr opened this issue Feb 27, 2019 · 3 comments · May be fixed by #150

Comments

@mactyr
Copy link

mactyr commented Feb 27, 2019

When I change the [value] of a select2 with the "multiple" attribute turned on, options in the new value that were not previously selected become selected, but all options that were previously selected also remain selected -- so my selection is the union of the old values and the new values, rather than just being the new values.

This also means that if I pass an empty array to [value], nothing changes, whereas I would expect all options to be un-selected.

I believe the issue is in setElementValue(), the innermost if needs an else to turn the selected property off when the option is not contained in the new value, like so:

if (newValue.indexOf(option.value) > -1) {
  this.renderer.setElementProperty(option, 'selected', 'true');
} else {
  this.renderer.setElementProperty(option, 'selected', null);
}
@mactyr
Copy link
Author

mactyr commented Feb 27, 2019

Or I suppose the whole block could just collapse to:

this.renderer.setElementProperty(option, 'selected', newValue.indexOf(option.value) > -1 ? 'true' : null);

@fflores-origin
Copy link

yes actually adding that fix solves the problem, hope @NejcZdovc could update this plugin

@fflores-origin fflores-origin linked a pull request Apr 30, 2020 that will close this issue
@tyspice
Copy link

tyspice commented May 11, 2021

This issue seems to have been resolved in a more actively maintained fork: ng-select2

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants