0

I'm having a form in vue js with select drop downs, I'm trying to use https://sagalbot.github.io/vue-select/ this library to execute this, according to the documentation I've to pass an array into this and I'm calling a axios method to get the options of the drop downs. I'm getting the data in following format:

Array of the values

And with following description:

Fields of my data

I want to only show the names in the option and get values as ID of that particular row, how can I achieve this.

My code look something like this: https://jsfiddle.net/eemdjLex/1/

<div id="app">
  <v-select multiple :options="model.data"></v-select>
</div>

import vSelect from 'vue-select';

Vue.component('v-select', vSelect)

const app = new Vue({
    el: '#app'
    router: router,
    data () {
        return {
            model: {},
            columns: {},
      }
    }
    methods: {
        fetchIndexData() {
                var vm = this;
                axios.get('/companies').then(response => {
                Vue.set(vm.$data, 'model', response.data.model)
                Vue.set(vm.$data, 'columns', response.data.columns)
            }
    }
});

It is not working proper but you get the idea what I'm trying to do.

2
  • Please show the code you've written to do this. Commented May 23, 2017 at 16:30
  • @thanksd: please check Commented May 23, 2017 at 16:39

3 Answers 3

1

v-select appears to return the entire option as the value when using v-model so I might use a pair of computed values here.

new Vue({
  el:"#app",
  data:{
    serverData,
    selected: null
  },
  computed:{
    // serverData is a stand in for your model.data.
    // map over that to build your options
    selectOptions(){
      return this.serverData.map(d => ({label: d.name, value: d.id}))
    },
    // selectedOption is just a short computed to get the id value
    // from whatever option was selected. You could also just use
    // "selected.id" in whatever needs the id instead if needed.
    selectedOption(){
      if (this.selected)
        return this.selected.value
      else
        return null
    }
  }
})

Example.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped.
1

Looking at the README on the GitHub page for vue-select, I'm seeing that you can pass the <v-select> component an options property as an array of objects with label and value keys.

I would make a computed property to take your model.data and format it this way:

computed: {
  options() {
    let data = this.model.data;
    let options = [];

    // do whatever you need to do to format the data to look like this object:
    // options = [{ label: 'foo', value: 1 }, { label: 'bar', value: 2 }]

    return options;
  }
}

Then pass this computed property to the <v-select> instead:

<v-select multiple :options="options"></v-select>

2 Comments

I need to iterate the model.data and store in options?
yeah, i don't know what your model data looks like, but if it's an array, you'd iterate through it, and for each element, push an object to the options array with a label and value property set to whatever properties of the element you want to be the label and value.
1

The official select docs may help you

your v-select component should look like

new Vue({
  template: `
     <select v-model="selected">
       <option v-for="option in options" v-bind:value="option.value">
         {{ option.text }}
       </option>
     </select>
     <span>Selected: {{ selected }}</span>`,
  el: 'v-select',
  props: [ 'options' ]
  data: {
    selected: ''
  }
})

2 Comments

This is the right idea, but he's using a custom v-select component, not a select element.
That's true. I've already being using as per normal select docs and I know it can be implemented like this but I'm using v-select component

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.