Vue: Support object values in `v-model` for <select>

Created on 4 Aug 2015  Â·  14Comments  Â·  Source: vuejs/vue

When using the {text:'', value:''} format for dynamic <select> options it becomes complex to get the text back out again for display since the v-model binds the value. One must then run a computed property and filter the options again and... you get the point.

Would it be possible to to support a syntax like this?

options: [
  {this: 'This One'},
  {that: 'That One'}
]

Which would then let us get that value back out like this.options[this.selected]. Just an idea. Feel free to say nay.

feature request intend to implement

Most helpful comment

@app-sparkler afaik serialization like that is not necessary (at least not in 2017). In the docs they show the following example:

<select v-model="selected">
  <!-- inline object literal -->
  <option v-bind:value="{ number: 123 }">123</option>
</select>

From here: https://vuejs.org/v2/guide/forms.html#Select-Options

All 14 comments

A better option might be to allow the option's value to be an actual object, like Angular does.

Yeah that would definitely work too.

This would be cool!

Installed latest code in dev branch, but it still doesn't work.
As I debug the code, I find that inside function buildOptions of select.js
After execute line 144 el.value = op.value, el.value is "[object Object]", a string value.

@J-F-Liu of course el.value will be serialized. The object value only works when syncing back to model.

The example code in #1183 still doesn't work, can you have a try?

@J-F-Liu did you build?

The dist folder contains the standalone build for Vue.js, however files here are only checked-in when a release happens. If you are on the dev branch, files here are NOT up to date.

@simplesmiler No, I installed with bower install yyx990803/vue#dev, after clone the repo and run build task the code works now, thanks.

@yyx990803

As I needed objects in my selected value, i worked with JavaScript's JSON methods

#STRINGIFY the input with JSON.stringify(myObject)

<select @change="statusSelected">
  <option v-for="orderStatus in orderStatusList", :value="JSON.stringify(orderStatus)"> {{ orderStatus.status }}</option>
</select>

#PARSE it with JSON.parse(myObjectString)

...
methods: {
  statusSelected (evt) {
    console.log(JSON.parse(evt.target.value))
  }
}
...

@app-sparkler afaik serialization like that is not necessary (at least not in 2017). In the docs they show the following example:

<select v-model="selected">
  <!-- inline object literal -->
  <option v-bind:value="{ number: 123 }">123</option>
</select>

From here: https://vuejs.org/v2/guide/forms.html#Select-Options

It seems like the support for objects as values is dropped again.

[Vue warn]: Invalid prop: type check failed for prop "value". Expected String, Boolean, Number, Array, got Object.

@ndabAP

It does work...

<template id="my-temp">
    <div>
        <h1>{{myProps.foo}}</h1>
    </div>
</template>

+

Vue.component('myComp', {
    template:'#my-temp',
    props: ['myProps'],
})

+

<my-comp my-props="{foo: 'bar'}" ></my-comp>

gives the output of :

<h1>bar</h1>

screenshot:

image

Good Luck...

So, still no sollution to this?

@zealotrahl please see my answer above your question.

Was this page helpful?
0 / 5 - 0 ratings