I want to create a simple accordion like structure but somehow I can't manage to toggle individual elements:
<div v-for="qa, j in group.questions_answers" :key="j">
<div class="question" @click="toggle()" > <!-- use index here? -->
<span v-if="itemOpen" class="font-iconmoon icon-accordion-up"><span/>
<span v-else class="font-iconmoon icon-accordion-down"><span/>
<span class="q-text" v-html="qa.question"><span/>
</div>
<div v-if="itemOpen" class="answer" v-html="qa.answer"></div>
</div>
How would I go about toggling indiviual question/answer blocks? Do I need to use refs?
Currently I can toggle all elements ...
export default {
data() {
return {
itemOpen: true
}
},
methods: {
toggle() { // index
this.itemOpen = !this.itemOpen
}
}
}
I could draft up a Q/A component and then do toggle inside of each Component but I believe that is an overkill for this.
UPDATE I am dealing with following data structure:
{
"intro_text": "intro text",
"questions_groups": [
{
"group_heading": "heading",
"questions_answers": [
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{...}
]
},
{
"group_heading": "heading 1",
"questions_answers": [
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{...}
},
{
"group_heading": "heading 2",
"questions_answers": [
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
},
{
"question": "Question",
"answer": "Answer"
}
{...}
]
}
]
}