Is there a way to toggle the visibility of the nearest div element by clicking a button in vue
?
Lets imagine I have a collection of items, each containing hidden information.
<ul>
<li v-for="item in items" :key="item.id">
<div>
<p>{{item.text}}</p>
<button @click="showDetails(item)">Show details</button>
<div class="details" :class="isVisible ? activeClass : 'hidden'">Some hidden details</div>
</div>
</li>
</ul>
Then I implement:
data() {
return {
items: [ // list of item objects here]
isVisible: false,
activeClass: 'is-visible'
}
},
methods: {
showDetails(item) {
this.isVisible = item;
}
}
Currently, when I click on a "showDetails" button, all divs with the class .details
open and acquire the .is-visible
class. However, I only want the closest div related to the item to be shown. It seems like it should be simple, but I can't get it to function properly.
How can I make that happen?