Vue Js中的复选框数组 [英] Checkbox array in Vue Js

查看:37
本文介绍了Vue Js中的复选框数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框数组,它们来自存储所有系统设置的主系统对象.(称为getSystem {}).

I have an array of checkboxes, coming from a main system object where I store all system setting. (called getSystem{}).

以这种形式,我访问一个具有一系列角色[]的用户.如何对照getSystem.user_roles检查此角色数组?

In this form, Im accessing a User, which has an array of roles []. How can I check this array of roles, against the getSystem.user_roles?

我知道如何使用javascript正常进行操作.但是我应该在复选框输入Vue.js中添加什么呢?

I know how to do it normally, in javascript obviously. But what would I put in the checkbox input Vue.js wise?

    <b-form-group>
      <label for="company">Email Address</label>
      <b-form-input type="text" id="email" v-model="user.email" placeholder="Enter a valid e-mail"></b-form-input>
    </b-form-group>
    // Here i can do user.roles to get the array of roles.
    // What can I do to loop through the roles and check the box if it exists in the user roles??
    <b-form-group v-for="resource, key in getSystem.user_roles" v-if="getSystem.user_roles">
       <label>{{resource.role_name}}</label>
       <input type="checkbox" [ what do I put here to compare against user.roles, and check the box if exists??]  > 
    </b-form-group>

推荐答案

这里有一个模拟您的逻辑的小例子

Here a little example emulating your logic

new Vue({
  el: '#app',
  data: {
    user: {
      email: 'test@test.com',
      roles: [{id: 1, name: 'Client'}]
    },
    roles: [
      {
        id: 1,
        name: 'Client',
      },
      {
        id: 2,
        name: 'Admin',
      },
      {
        id: 3,
        name: 'Guest',
      }
    ]
  }
})

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>

<div id="app">
  <div>
    <label>Email</label>
    <input type="text" v-model="user.email" />
  </div>
  <div v-for="role in roles" :key="role.id">
    <label>{{role.name}}</label>
    <input type="checkbox" v-model="user.roles" :value="role"/>
  </div>
  
  <p>User's selected roels</p>
  {{user.roles}}
</div>

这篇关于Vue Js中的复选框数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆