3

I'm using Vue.js 2.0 and the Element UI library.

I'm working with a multiple select component. The v-model property is here to pre-select any options. So if you have model: ['Option4'], the select will be preselected with Option4

I would like to be able to v-model an array of object instead of simply an array containing the label of each option.

That is to say instead of usingmodel: ['Option4'] I would like to be able to use something like model: [{name:'Option4'}, {name:'Option5'}].

When doing so the pre-selection is not made properly.

Is it possible to do that ? If so how ?

http://jsfiddle.net/3ra1jscx/

<div id="app">
<template>
  <el-select v-model="model" multiple placeholder="Select">
    <el-option v-for="item in options" :label="item.label" :value="item.value">
    </el-option>
  </el-select>
</template>
</div>

var Main = {
    data() {
      return {
        options: [{
          value: 1,
          label: 'Option1'
        }, {
          value: 2,
          label: 'Option2'
        }, {
          value: 3,
          label: 'Option3'
        }, {
          value: 4,
          label: 'Option4'
        }, {
          value: 5,
          label: 'Option5'
        }],
        model: ['Option4']
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
5
  • why do you remove accepted answer from my answer ? Commented Apr 5, 2017 at 20:12
  • Hello Alexandru, I followed your advice and now I'm putting it into context. The preselection is ok but I can't select another option. Any idea ? Commented Apr 5, 2017 at 20:20
  • I don't understant why you can't select another option.In edited fiddle it is working perfectly.Have a look. Commented Apr 5, 2017 at 20:30
  • I'm going to create a new task for that. I'm sorry. Yes this one is working perfectly but the issue I posted here does not illustrate the whole story. Maybe you will be able to help me on the next one I'm going to create. Commented Apr 5, 2017 at 20:33
  • stackoverflow.com/questions/43241149/… Commented Apr 5, 2017 at 20:43

1 Answer 1

1

You should pass the value of object in options array.

var Main = {
  data() {
    return {
      options: [{
        value: 1,
        label: 'Option1'
      }, {
        value: 2,
        label: 'Option2'
      }, {
        value: 3,
        label: 'Option3'
      }, {
        value: 4,
        label: 'Option4'
      }, {
        value: 5,
        label: 'Option5'
      }],
      model: [4]
    }
  }
}

Here is working solution.

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

Comments

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.