Jiel
Jiel

Reputation: 123

Vue Select how to bind 1 property to v-model

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: '',
  }
})

this will result to this enter image description here

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

Answers (3)

Saurabh Mistry
Saurabh Mistry

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

Mike Diglio
Mike Diglio

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

NeonNatureEX
NeonNatureEX

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

Related Questions