In Vue how can I set checkboxes to be disabled depending on its value?

Viewed 1577

I am looping through checkboxes to build a seat selector which has a value from A1 to A10. I also have an array in the data property defining the values I would like to set to be disabled. If a value inside this array equals a value of a checkbox, it should be disabled. I can only seem to access the value of the checkbox when it has been checked. How can I get the value of every checkbox?

<span class="seat" v-for="n in 10" :key="n">
  <label class="checkbox-label">
    <input
    class="checkbox-input"
      type="checkbox"
      v-model="selected"
      :value="'A' + n"
      @change="check($event)"
      :disabled="isDisabled" 
      true-value="1" false-value="0"
      number> {{ n }} 
      <span class="checkmark"></span>
  </label>
</span>

data() {
  return {
    selected: [],
    disabled: ['A1', 'A2', 'A9']
  }
},
computed: {
  isDisabled() {
    // limit seats picked to number of tickets chosen
    if (this.selected.length > (this.totalTickets - 1)) {
      return true
    } else {
      return false
    }
  },
},
mounted: function() {
  //disable checkboxes here?
},
1 Answers

Try to access $event target value and check if it's included in the disabled array :

 :disabled="disabled.includes($event.target.value)" 

or add method with the n as a parameter :

 :disabled="disable(n)"

in methods :

computed:{
 ....
},
methods:{
  disable(n){
   return this.disabled.includes('A'+n);
}
}
Related