Open genuinefafa opened 7 years ago
I can confirm this issue.
The problem is that the val()
call in the select2:unselect
handler still returns the old value. That seems to be an issue with select2.
The event I get on clearing carries the element being removed:
I can think of a workaround if multiple: false
-- just return a null result on every unselect because there can't be anything left. I still wonder why select2 4.0.3 returns the old values in the select2:unselect
event handler.
I did "solve it" by using an addon
button with the X "simulating" the desired behavior... it might work for you as well @achimha ;)
could you share your workaround please, thanks :)
I use this workaround for now:
this.element.on('select2:select', () => {
this.onChange(this.element.val());
this.onTouched();
this.valueChanged.emit({
value: this.element.val(),
data: this.element.select2('data')
});
});
this.element.on('select2:unselect', () => {
/* for some reason the element is still returned by val. Workaround for single-select controls */
if (this.options.multiple !== true) {
this.onChange(null);
this.onTouched();
this.valueChanged.emit({
value: null,
data: []
});
return;
}
this.onChange(this.element.val());
this.onTouched();
this.valueChanged.emit({
value: this.element.val(),
data: this.element.select2('data')
});
});
how can I use that workaround in my code @achimha?
I use a modified version of the component with that (and some other stuff) changed. If you want, you can point to it in your package.json
: https://github.com/achimha/ng2-select2
that's it @achimha, i'm using it... 👍
somehow the theme
behavior is different than the old one... but this one, feels better. Cheers!
pst: why not do a PR?
To install it from @achimha repo
npm install -save git+https://github.com/achimha/ng2-select2
The other workaround is to store value on change and check if the other time event triggered with the same value (which isn't possible during actual change).
Is this issue posted on select2?
how can i fire select2-unselected event?
still having this issue
Using ng2-select2@1.0.0-beta.10 I found that
allowClear: true
option allows to clear selection in the UI, but thevalueChanged
event is firing incorrectly with the value of the last selected option instead of an empty one.