4

how can I disable an array of checkbox after checking a number of checkboxes from that array ? Is there a way to do this in vuejs or I will need a watcher for that? In fact I tried to watch the 'ord.sauce' but I couldnt make it work

this is the component

Vue.component('sauce-view', {
props: ["sauce", "ord", "name"],
template: '
    <div class="">
            <input type="checkbox" :name="name" :value="sauce.id" v-model="ord.sauce">  
            {{sauce.name}}
        <label >
            <img :src="sauce.link" >
        </label>
    </div>',
  });

This is the HTML

<table>
<tr v-for="o in order" >
       {{o.sauce}}      
     <td v-for="sauce in les_sauces" >
        <sauce-view :sauce="sauce" :ord="o" :name="o.produit+o.num">
        </sauce-view>
    </td>
</tr></table>

2 Answers 2

12

I have created a simple fiddle that should illustrate the logic behind it: https://jsfiddle.net/UDany/r9q4x85d/

This would be the markup:

<div id="demo">
  <template v-for="(item, index) in itemlist">
    <label><input type="checkbox" :value="index" name="condiment" v-model="selectedItems" :disabled="selectedItems.length >= max && selectedItems.indexOf(index) == -1" /> {{item}}</label>
  </template>
  <div>{{selectedItems.join(', ')}}</div>
</div>    

And your JS would look like this:

var demo = new Vue({
    el: '#demo',
    data: {
        selectedItems: [],
        itemlist: [
                "Mayo",
                "Ketchup",
                "Mustard",
                "Honey"
        ],
        max: 2
    }
})

Since you're not using the input directly into the main code you'll need to proxy the properties through/from it (possibly wiring events for "select" and "unselect" and having a property for "disabled")

Sign up to request clarification or add additional context in comments.

2 Comments

why do we need this "selectedItems.indexOf(index) == -1" in the condition?
@brahimm this makes sure it doesn't disable the currently checked checkboxes. In an array indexOf returns the index of the value it receives OR -1 if it wasn't able to find it, thus "selectedItems.indexOf(index) == -1" will only return true if the value can't be found within the selectedItems array.
0

I've answered a question very similar to this here:

Vue.js limit selected checkboxes to 5

In your situation it's a little more involved since you're handling the value in a component. It's hard to tell your exact needs so I'd clear up your question a little more.

2 Comments

the idea is clever, I didnt think of it first time, but it disables all the inputs, and I need the checked-boxes to stay able to be unchecked to that I can control that the user check only two (for example)
The code in that answer doesn't disable checked inputs so you'll always be able to unselect them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.