Search code examples
arraysvue.jscheckboxlist

vuejs model for checkbox group


I have 2 arrays one for possible checkbox variants and one for already saved-checked boxes.VUEJS template for example

<ul>
                <li v-for="cit in possable">
                    <label>
                    <input  
                        type="checkbox"
                        :checked="savedcbx.indexOf(+cit.id)>-1"
                        :value="cit.id"/>
                    {{cit.rname}}
                    </label>
                </li>
            </ul>

And my question about how add new checkedbox to saved array or delete from saved array uncheckedbox&


Solution

  • I just put savedcbx to model and it works like in answer above. Vue great thing.

                 <ul>
                <li v-for="cit in possable">
                    <label>
                    <input  
                        type="checkbox"
                        v-model="savedcbx"
    
                        //with model this not need too 
                        :checked="savedcbx.indexOf(+cit.id)>-1"
                        :value="cit.id"/>
                    {{cit.rname}}
                    </label>
                </li>
            </ul>