3
votes

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?

2

2 Answers

2
votes

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

i had faced the same problem before and i didn't find a good solution, but i had tried something like the following :

new Vue({
  el: "#app",
  data: {
    selectAll: false,
    eachCheckbox: [],
  },
  methods: {
    selectAllItems() {

      this.selectAll ? this.eachCheckbox = ["Item 1", "Item 2", "Item 3"] : this.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" @change="selectAllItems">
    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>