Skip to content
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

Different behaviour of :value and v-model in a select tag when dynamically adding options while none is yet selected #4810

Closed
jonjanisch opened this issue Jan 29, 2017 · 6 comments

Comments

@jonjanisch
Copy link

jonjanisch commented Jan 29, 2017

My custom "select" component behaves differently when compared to simply using a raw select element in my template.

When the custom select's value is null and the options are loaded async, the first option is automatically selected.

Expected: The custom select's "selected" value should remain empty. This is the behavior when using a raw select in the template (not a custom component).

Here's a simple fiddle illustrating the issue:
https://jsfiddle.net/33df9qu8/3/

Custom select bug

If you uncomment the option with a null optionId in my fiddle:

{ text: 'Not selected', optionId: null },

...the select behaves perfectly fine and selects that option. But that's not really a workaround as it forces us to add a null option and changes our UX.

It may be related to these similar issues here:
#1008
#3673

Vue.js version

2.1.10

Chrome version

55.0.2883.95 (64-bit)

Reproduction Link

https://jsfiddle.net/33df9qu8/3/

@posva
Copy link
Member

posva commented Jan 29, 2017

The behaviour is different because of the value binding instead of the v-model.
As a workaround you can use a computed property in the custom select and use the v-model directive instead of :value
https://jsfiddle.net/dux6q43p/

@posva posva changed the title Custom "select" component does not handle null value properly Different behaviour of :value and v-model in a select tag when dynamically adding options while none is yet selected Jan 29, 2017
@jonjanisch
Copy link
Author

Thank you. The workaround worked great but I'm not quite sure why it works. Is there anywhere in the docs where this is explained further?

@jonjanisch
Copy link
Author

jonjanisch commented Jan 29, 2017

I noticed that neither my custom select nor the workaround demonstrated by @posva handle a null option properly (contrary to what I originally stated in my first comment).

{ text: 'Yellow', optionId: 'YELLOW'},
{ text: 'Not selected', optionId: null }, // <--- null option
{ text: 'Blue', optionId: 'BLUE'}

If the original select value is null, it properly selects Not Selected when the options are added dynamically initially. However, if you toggle to Blue and then back to Not selected, it will show an empty combobox (instead of Not Selected).

EDIT: For completeness I found a workaround for this problem as well. But it would be nice if component developers did not have to do this. I've updated the fiddle with the following workaround:

onChange(value) {
  if (value === '') {
    value = null;
  }
  this.$emit('input', value);
}

See here:
https://jsfiddle.net/dux6q43p/1/

@posva
Copy link
Member

posva commented Jan 30, 2017

That's actually because the value of the select dom element is always a string. So instead of being null, it's just an empty string

@tiagomatosweb
Copy link

tiagomatosweb commented Feb 10, 2017

I think I'm experiencing something similar to this issue. I have asked in here, but not luck yet https://forum.vuejs.org/t/vuejs-ajax-populating-select-and-weird-behaviour/6222.

However I could figure out any workaround! Can someone give me a clue?
Much appreciated.

@yyx990803
Copy link
Member

Closing due to inactivity (also because a workaround has been pointed out).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants