Jose Seie
Jose Seie

Reputation: 890

in vueJS, how to bind the Id of selected option in v-select v2.5.1

It's been hours that I'm trying to get the id of the selected option in v-select, but it returns me the object not the id. Is there any way to get only the Id (object Id) of the selected option?

I already checked the documentation site: https://sagalbot.github.io/vue-select/docs/

I also checked the various examples in: https://codepen.io/collection/nrkgxV/

But so far I have not found the concrete solution to my problem. What is missing or am I doing wrong?

My code:

<template>
    <div>
        <v-select 
            v-model="selectedId"
            :options="items"
            label="name"
        ></v-select>
    </div>         
</template>

<script>
export default {
    data () {
    return {
        items: [
            {id: 1, name: "User 1", creator_id: 3},
            {id: 2, name: "User 2", creator_id: 1},
            {id: 4, name: "User 3", creator_id: 3},
        ],
        selectedId: '',
        ...    
        }
 }

Upvotes: 5

Views: 9530

Answers (5)

Priyanka Shelar
Priyanka Shelar

Reputation: 31

Returning a single key with reduce

f you need to return a single key, or transform the selection before it is synced, vue-select provides a reduce callback that allows you to transform a selected option before it is passed to the @input event. Consider this data structure:

let options = [{code: 'CA', country: 'Canada'}];

If we want to display the country, but return the code to v-model, we can use the reduce prop to receive only the data that's required.

<v-select :options="options" :reduce="country => country.code" label="country" />

https://vue-select.org/guide/values.html#transforming-selections

Upvotes: 2

dhufish
dhufish

Reputation: 151

In the v-select add these attributes

item-value="id" item-text="name"

Upvotes: 3

Arno van Oordt
Arno van Oordt

Reputation: 3520

It took me quite a while to figure out but apparently you can use :reduce="item = item.id"

See: https://vue-select.org/guide/values.html#getting-and-setting

A real life saver since the "computed" approach wasn't gonna cut it in my case

Upvotes: 3

Sovalina
Sovalina

Reputation: 5609

Instead of using v-model , you can listen the event on the select:

Vue.component("v-select", VueSelect.VueSelect);
new Vue({
  el: "#app",
  data () {
    return {
      items: [
        {id: 1, name: "User 1", creator_id: 3},
        {id: 2, name: "User 2", creator_id: 1},
        {id: 4, name: "User 3", creator_id: 3},
      ],
      selectedId: '' 
    }
  },
  methods: {
    selectId(e) {
      this.selectedId = e.id
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-select/2.5.1/vue-select.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
  <v-select 
    @input="selectId($event)"
    :options="items"
    label="name">
  </v-select>
  <p>Selected ID: {{ selectedId }}</p>
</div>

Upvotes: 4

Emma
Emma

Reputation: 116

how about add a computed props id

<script>
    export default {
        data () {
            return {
                items: [
                    {id: 1, name: "User 1", creator_id: 3},
                    {id: 2, name: "User 2", creator_id: 1},
                    {id: 4, name: "User 3", creator_id: 3},
                ],
                selectedId: {}  
            }
        },
        computed: {
            id: function () {
                return (this.selectedId && this.selectedId.id)?this.selectedId.id:'';
            }
        }
    }
</script>

Upvotes: 2

Related Questions