NejcZdovc / ng2-select2

Select 2 wraper for angular2
MIT License
123 stars 93 forks source link

Clearing selection doesn't fire with undefined/null value #61

Open genuinefafa opened 7 years ago

genuinefafa commented 7 years ago

Using ng2-select2@1.0.0-beta.10 I found that allowClear: true option allows to clear selection in the UI, but the valueChanged event is firing incorrectly with the value of the last selected option instead of an empty one.

achimha commented 7 years ago

I can confirm this issue.

achimha commented 7 years ago

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.

achimha commented 7 years ago

The event I get on clearing carries the element being removed:

screen shot 2017-02-24 at 18 02 04 screen shot 2017-02-24 at 18 02 16

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.

genuinefafa commented 7 years ago

I did "solve it" by using an addon button with the X "simulating" the desired behavior... it might work for you as well @achimha ;)

tsifuentes commented 7 years ago

could you share your workaround please, thanks :)

achimha commented 7 years ago

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')
            });
        });
genuinefafa commented 7 years ago

how can I use that workaround in my code @achimha?

achimha commented 7 years ago

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

genuinefafa commented 7 years ago

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?

ramandv commented 7 years ago

To install it from @achimha repo

npm install -save git+https://github.com/achimha/ng2-select2

inossidabile commented 7 years ago

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).

theredcameron commented 7 years ago

Is this issue posted on select2?

masoudline commented 7 years ago

how can i fire select2-unselected event?

abeninski commented 6 years ago

still having this issue