Reputation: 123
I've put v-model in v-select but it returns the whole object
<div id="app">
<h1>Vue Select - Using v-model</h1>
<v-select v-model="selected" :options="options" value="id" label="labels">
</v-select>
{{selected}}
</div>
Vue.component('v-select', VueSelect.VueSelect)
new Vue({
el: '#app',
data: {
options: [
{id: 1, labels: 'foo'},
{id: 3, labels: 'bar'},
{id: 2, labels: 'baz'},
],
selected: '',
}
})
is there a way to get the selected objects id only instead of the whole object? I've tried putting value="id" but still doesn't work.
Upvotes: 2
Views: 17190
Reputation: 13689
@Jiel , here is the working demo
Vue.component('v-select', VueSelect.VueSelect);
var app = new Vue({
el: '#app',
data: {
selected:'',
options: [
{ id: 0, labels: 'Vegetables' },
{ id: 1, labels: 'Cheese' },
{ id: 2, labels: 'Fruits' }
]
},
computed: {
selectedID: function () {
return this.selected.id
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/vue-select@latest"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<div id="app">
<h1>Vue Select - Using v-model</h1>
<v-select v-model="selected" :options="options" label="labels">
</v-select>
selectedID : {{selectedID}}
</div>
Upvotes: 0
Reputation: 735
Your best option would be to use a computed
property so you can manipulate selected
to return your requested value:
computed: {
selectedID: function () {
return this.selected.id;
}
}
Working Codepen with your example
Upvotes: 2
Reputation: 497
Do you mean
<v-select v-model="selected" :options="options" id=" {{selected.id}} " label="labels">
?
This will bind the selected ID into your V-select.
Upvotes: 0