简体   繁体   English

Vue Js中的复选框数组

[英]Checkbox array in Vue Js

I have an array of checkboxes, coming from a main system object where I store all system setting.我有一系列复选框,来自主系统 object,我存储所有系统设置。 (called getSystem{}). (称为 getSystem{})。

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?如何对照 getSystem.user_roles 检查这个角色数组?

I know how to do it normally, in javascript obviously.我知道如何正常进行,显然在 javascript 中。 But what would I put in the checkbox input Vue.js wise?但是我会在复选框输入 Vue.js 明智地输入什么?

    <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>

This behavior is well documented on the Checkbox binding Docs. Checkbox binding Docs 中有详细记录此行为。

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>

<input type="checkbox" v-model="userRoles" :true-value="[]" :value="resource.role_name">

您应该添加:true-value="[]"

We can use dynamic check box input rendering with the condition to select values from the customized function (in my example selectUsers ).我们可以使用带有条件的动态复选框输入渲染来从自定义函数中选择值(在我的示例中selectUsers )。 In that function, we can write conditions that we need to compare before append to the selected array.在该函数中,我们可以编写在追加到所选数组之前需要比较的条件。

Demo:演示:

在此处输入图像描述

This is the full NuxtJs(vue) component with dummy data.这是带有虚拟数据的完整 NuxtJs(vue) 组件。

<template>
  <v-container fluid>
    <p>{{selected }}</p>
    <div v-for="user in user_roles" :key="user[0]">
    <input type="checkbox"
      @click="()=>{selectUsers(user[0])}"
      :value="user[0]"
    >
    {{user[1]}}
    </div>
  </v-container>
</template>

<script>
import VueLodash from 'vue-lodash'
import lodash from 'lodash'
export default {
    
    data() {
    return {
      user_roles:[[1,"dd"],[2,"ddd"],[3,"kksse"],[4,"kske"]] ,
      selected:[],
    };
  },
  methods: {
      selectUsers(id){
          //in here you can check what ever condition  before append to array.
          if(this.selected.includes(id)){
              this.selected=_.without(this.selected,id)
          }else{
              this.selected.push(id)
          }
      }
  }
}
</script>
<b-form-group v-for="resource, key in getSystem.user_roles" v-if="getSystem.user_roles" :key="key">
   <label>{{resource.role_name}}</label>
   <input type="checkbox" v-model="userRoles" :value="resource.role_name"  > 
</b-form-group>

<script>
  data(){
    return {
      userRoles: []
    }
  }
</script>

In my case, as I am server rendering the checkboxes, what worked for me is to replace :value with just value就我而言,当我在服务器渲染复选框时,对我有用的是将:value替换为value

@foreach($carOptions as $option)
<div class="form-check">
    <input class="mx-2 form-check-input cursor-pointer"
           type="checkbox"
           value="{{$option}}" <------ HERE in order for vue to work I had to change from :value to value
           v-model="offer.carOptions">
    <label class="custom-control-label cursor-pointer"
           for="leading">{{ __($option) }}</label>
</div>
@endforeach

NOTE: The code snippet is a Laravel Blade Templates.注意:代码片段是 Laravel 刀片模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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