I would like to implement a feature where each post in a loop has a dropdown menu that can be shown/hidden when clicked:
<div v-for="(post, index) in posts" :key="index" >
<div v-on:click.prevent="toggleDropDown(post)">Show/hide menu
</div>
<div v-if="post.showDropDown">
<ul class="menu">
<li><a href="#" >Edit</a></li>
<li><a href="#" >Delete</a></li>
</ul>
</div>
<div>
{{post.body}}
</div>
</div>
Here is the method I am using:
toggleDropDown(post) {
if (!post.showDropDown) {
post.showDropDown =true;
} else {
post.showDropDown =false;
}
},
However, this implementation is not working as expected. When I click on Show/hide menu
, nothing happens.
It should be noted that the data for posts
is fetched from the server and the post
object does not contain a showDropDown
field initially.
If I use v-if="post.showDropDown"
, all menus open/close together. This behavior is not desired.
How can I correct this issue?