简体   繁体   中英

Multiple select Vue.js and computed property

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

I want to use a multiple select to attribute some roles to my users.

The list of all roles available is received and assigned to availableRoles . Since it is an array of object and the v-model accepts only an array with value, I need to extract the id of the roles trough the computed property computedRoles .

The current roles of my user are received and assigned to userRoles: [{'id':1, 'name':'Admin'}, {'id':3, 'name':'User'}] .

computedRoles is then equals to [1,3]

The preselection of the select is fine but I can't change anything (add or remove option from the select)

What is wrong and how to fix it?

http://jsfiddle.net/3ra1jscx/3/

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

var Main = {
    data() {
      return {
        availableRoles: [{
          id: 1,
          name: 'Admin'
        }, {
          id: 2,
          name: 'Power User'
        }, {
          id: 3,
          name: 'User'
        }],
        userRoles: [{'id':1, 'name':'Admin'}, {'id':3, 'name':'User'}]
      }
    },

    computed : {

        computedRoles () {
            return this.userRoles.map(role => role.id)
        }
    }
  }

Check the solution: jsfiddle

The caveat here is that computed properties are getters mainly. You can define setter for computed property, but my approach is more vue-like in my opinion.

In short, instead of v-model on computed set v-model for data property.

Full code:

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui/lib/index.js"></script>
<div id="app">
<template>
  <el-select v-model="ids" multiple placeholder="Select" @change="logit()">
    <el-option v-for="item in availableRoles" :label="item.name" :value="item.id">
    </el-option>
  </el-select>
</template>
</div>

var Main = {
    data() {
      return {
        availableRoles: [{
          id: 1,
          name: 'Admin'
        }, {
          id: 2,
          name: 'Power User'
        }, {
          id: 3,
          name: 'User'
        }],
        userRoles: [{'id':1, 'name':'Admin'}, {'id':3, 'name':'User'}],
        ids: []
      }
    },
    mounted() {
        this.ids = this.userRoles.map(role => role.id);
    },
    methods: {
        logit: function() {
        console.log(this.ids);
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

I agree mostly with @wostex answer, but he doesn't give you the userRoles property back. Essentially you should swap computedRoles and userRoles . userRoles becomes a computed property and computedRoles is a data property. In my update, I changed the name of computedRoles to selectedRoles .

var Main = {
    data() {
      return {
        availableRoles: [{
          id: 1,
          name: 'Admin'
        }, {
          id: 2,
          name: 'Power User'
        }, {
          id: 3,
          name: 'User'
        }],
        selectedRoles:[1,2]
      }
    },
    computed : {
      userRoles(){
         return this.availableRoles.reduce((selected, role) => {
             if (this.selectedRoles.includes(role.id))
                    selected.push(role);
             return selected;
         }, [])
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

And here is the fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM