ng2-select2 icon indicating copy to clipboard operation
ng2-select2 copied to clipboard

Clearing selection doesn't fire with undefined/null value

Open genuinefafa opened this issue 8 years ago • 14 comments

Using [email protected] 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.

genuinefafa avatar Feb 17 '17 15:02 genuinefafa

I can confirm this issue.

achimha avatar Feb 24 '17 15:02 achimha

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 avatar Feb 24 '17 15:02 achimha

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.

achimha avatar Feb 24 '17 17:02 achimha

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

genuinefafa avatar Feb 24 '17 19:02 genuinefafa

could you share your workaround please, thanks :)

tsifuentes avatar Feb 24 '17 23:02 tsifuentes

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')
            });
        });

achimha avatar Feb 25 '17 06:02 achimha

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

genuinefafa avatar Mar 03 '17 15:03 genuinefafa

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

achimha avatar Mar 03 '17 21:03 achimha

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?

genuinefafa avatar Mar 03 '17 22:03 genuinefafa

To install it from @achimha repo

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

ramandv avatar Mar 06 '17 08:03 ramandv

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

inossidabile avatar Mar 15 '17 00:03 inossidabile

Is this issue posted on select2?

theredcameron avatar Jun 18 '17 00:06 theredcameron

how can i fire select2-unselected event?

masoudline avatar Oct 01 '17 09:10 masoudline

still having this issue

abeninski avatar Sep 04 '18 18:09 abeninski