I am struggling to create a simple accordion-like structure with the ability 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 can I toggle individual question/answer blocks without affecting all elements? Should I use refs for this functionality?
At present, I am able to toggle all elements at once...
export default {
data() {
return {
itemOpen: true
}
},
methods: {
toggle() { // index
this.itemOpen = !this.itemOpen
}
}
}
Creating a separate Q/A component with internal toggling logic seems like an excessive solution for this issue.
UPDATE: My data structure is as follows:
{
"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"
}
{...}
]
}
]
}