vue.js - Toggle in loop? -


i wish toggle (show/hide) list when clicking on title, cant following work

i have this:

          <!-- title -->           <div v-for="(subitem, index) in item" v-if="index === 0" @click="toggle(subitem)">             {{subitem.system_name}} - ({{item.length}})           </div>            <!-- title items should expand on click "title" -->           <div v-if="subitem.clicked">             <p>{{subitem.system_name}}</p>           </div> 

when clicking on im triggering toggle function called toggle, sets property on item "clicked" true or false (i should mention property not exist on object, , haven't got possiblity add it, json api)

the toggle function this:

toggle: function (data) {     data.clicked = !data.clicked;   }, 

now, when above, error saying: "property or method "subitem" not defined on instance referenced during render. make sure declare reactive data properties in data option"

im guessing because "clicked" property doesnt exist in object... how work around this? can't see real solution ?

you initialize subitem in v-for single item in loop, using outside element has v-for loop on it. that's reason warning:

property or method "subitem" not defined on instance referenced during render. make sure declare  reactive data properties in data option" 

so move div want toggle inside div has v-for loop on it

      <!-- title -->       <div v-for="(subitem, index) in item" v-if="index === 0" @click="toggle(subitem)">         {{subitem.system_name}} - ({{item.length}})           <!-- title items should expand on click "title" -->           <div v-if="subitem.clicked">               <p>{{subitem.system_name}}</p>           </div>       </div> 

and coming yo 2nd issue, mention subitem obj not have clicked property when fetch json api.

you cannot add root level reactive properties after vue instance created.

since want toggle appeance of div based on property clicked not available @ time vue instance created should use vm.$set() add reactive properties or object.assign() add properties existing object. see reactivity in depth

so in case

toggle: function (data) {     if(data.hasownproperty('clicked')){         data.clicked = !data.clicked;     }else{         //since first time , set value pf clicked true show subitem         data = object.assign({}, data, {clicked: true});     }    }, 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -