Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
How to control multiple checkbox in Vue

I have a checkbox component in Vue:

<template>
    <div class="checkbox">
        <input class="checkbox-input" name="input" type="checkbox" v-model="checkbox">
    </div>
</template>
  
<script>
export default {
    data(){
        return {
            checkbox: false
        };
    },
};
</script>

So in the parent component I want to control these checkbox. So here is my parent component:

<div class="card">
    <div class="card-header">
        <CheckBox />
    </div>
<div class="card-body" v-for="item in cart" :key="item.product.id">
    <div class="checkbox-area">
        <CheckBox />
    </div>
</div>

So checkbox in card-body can be added when user clicks to add. So if a user clicks 3 times, 3 checkbox are being added inside of card-body. What I am trying to achieve is, as you see in card-header there is another checkbox, and when this checkbox is clicked, I want to check all the checkboxes inside card-body, and when it is unchecked in card-header, I want to unchcecked everything inside card-body.

So do you have any idea how to achieve this?

Thanks...

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can try like this :

Vue.component('checkbox', {
  template: `
    <div class="checkbox">
        <input class="checkbox-input" name="input" type="checkbox" @change="getCheck" v-model="value">
    </div>
  `,
  props: {
    checked: {
      type: Boolean,
      default: false
    }
  },
  data(){
    return {
      value: this.checked
    };
  },
  methods: {
    getCheck() {
      this.$emit("set-checked", this.value)
    }
  },
  watch: {
    checked(){
      this.value = this.checked
    }
  }
})

new Vue({
  el: '#demo',
  data(){
    return {
      all: false,
      cart: [
        {id: 1, check: false},
        {id: 2, check: false},
        {id: 3, check: true},
        {id: 4, check: false}
      ]
    };
  },
  watch: {
    cart() {
      this.cart.find(c => c.check === false) ? this.all = false : this.all = true
    }
  },
  methods: {
    checkAll(val) {
      this.all = val
      this.cart = this.cart.map(c => {
        c.check = val 
        return c
      })
    },
    checkItem(id) {
      this.cart = this.cart.map(c => {
        if(c.id === id) {
          c.check = !c.check 
        }
        return c
      })
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div class="card">
    <div class="card-header">
    <p>All</p>
      <checkbox :checked="all" @set-checked="checkAll" />
    </div>
    <br/>
    <div class="card-body" v-for="item in cart" :key="item.id">
      <div class="checkbox-area">
        <checkbox :checked="item.check" @set-checked="checkItem(item.id)" />
      </div>
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

First of all you need to add some props to your Component. than a wachter to emit a sync when the Value of the checkbox changes.

<template>
    <div class="checkbox">
        <input class="checkbox-input" name="input" type="checkbox" v-model="value">
    </div>
</template>

<script>
export default {
    props: {
        checked: {
            type: Boolean,
            default: false
        }
    }
    data(){
        return {
            value: this.checked
        };
    },
    watch: {
        value(){
            this.$emit("update:checked", this.value)
        }
    }
};
</script>

on the cart you need to watch for theses changes an than you can check/uncheck all the items.

<template>
    <div class="card">
        <div class="card-header">
            <CheckBox :checked.sync="global"/>
        </div>
        <div class="card-body" v-for="item in cart" :key="item.product.id">
            <div class="checkbox-area">
                <CheckBox :checked.sync="item"/>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data(){
        return {
            global: false,
            cart: [
                false,
                false,
                false
            ]
        };
    },
    watch: {
        global(){
            for (let i = 0; i < this.cart.length; i++) {
                this.chart[i] = this.global
            }
        }
    }
};
</script>

I have not tested this code, but this should work...

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda