Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check-all stops working when I add v-model attribute to the checkboxes

Check-all feature stops working when I try to get value of each checkbox in an array using v-model. I read lot of questions on different portals including stackoverflow, people are saying that v-model doesn't work with :checked attribute which I understand but could not find a solution / alternate code to make it work.

The 1st code that I tried was to select all checkboxes using the 1st checkbox. This works well. Code below:

new Vue({
  el: "#app",
  data: {
    selectAll:false
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <label>
    <input type="checkbox" v-model="selectAll">
    Select all
  </label>
  <br>
  <label>
    <input type="checkbox" :checked="selectAll" value="Item 1">
    Item 1
  </label>
  <br>
  <label>
    <input type="checkbox" :checked="selectAll" value="Item 2">
    Item 2
  </label>
  <br>
  <label>
    <input type="checkbox" :checked="selectAll" value="Item 3">
    Item 3
  </label>
</div>

The 2nd code that I tried was to get value of each checkbox in an array but in this case 'select all' automatically stops working. Code below:

new Vue({
  el: "#app",
  data: {
    selectAll:false,
    eachCheckbox: [],
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <label>
    <input type="checkbox" v-model="selectAll">
    Select all
  </label>
  <br>
  <label>
    <input type="checkbox" :checked="selectAll" value="Item 1" v-model="eachCheckbox">
    Item 1
  </label>
  <br>
  <label>
    <input type="checkbox" :checked="selectAll" value="Item 2" v-model="eachCheckbox">
    Item 2
  </label>
  <br>
  <label>
    <input type="checkbox" :checked="selectAll" value="Item 3" v-model="eachCheckbox">
    Item 3
  </label>
  <br>
  Selected checkbox values: {{eachCheckbox}}
  
</div>

I don't know how to make this work. Can someone help please?

like image 983
Pranav Kaistha Avatar asked Dec 22 '25 08:12

Pranav Kaistha


1 Answers

Use Vue.set to create objects in the checkbox array once an API call completes.

This shows a simulated async api call which takes 2.5 seconds to complete.

new Vue({
  el: '#app',
  data () {
    return {
      loading: false,
      checkall: false,
      checkboxes: []
    }
  },
  methods: {
    toggleAll () {
      this.checkall = !this.checkall
      this.checkboxes.forEach(c => {
        c.checked = this.checkall
      })
    }
  },
  watch: {
    checkboxes: {
      deep: true,
      handler: function () {
        this.checkall = this.checkboxes.every(c => c.checked)
      }
    }
  },
  mounted () {
    // simulate an async api call which takes 2.5 seconds to complete
    this.loading = true
    
    setTimeout(() => {
      Array.from(Array(3), (c, i) => ({ checked: false, text: `Option ${i + 1}` })).forEach((c, i) => {
        Vue.set(this.checkboxes, i, c)
      })
      this.loading = false
    }, 2500)
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <input type="checkbox" @click="toggleAll" v-model="checkall"/> Check All<br/>
  <div v-for="(c, i) in checkboxes" :key="i">
    <input type="checkbox" v-model="c.checked"/>{{ c.text }}<br/>
  </div>
  <p v-if="!loading">Checked: {{ checkboxes.filter(c => c.checked).map(c => c.text).join(',') }}</p>
  <p v-else>Fetching data...</p>
</div>
like image 160
Brian Lee Avatar answered Dec 23 '25 21:12

Brian Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!