I'm making a list of items with v-for loop. Inside each item of the loop there is button with click event method that showing description text. When i click on the button, it should toggle only inside it's own item, but it affecting all elements in v-for list.
So, how to make a toggle method that will affect only it's own item?
<template>
<div>
<div v-for="item in items" :class="{ activeclass: isActive }">
<div class="item-text">
{{item.text}}
</div>
<button @click="toggle()">show</button>
<div v-show="isActive" class="item-desc">
{{item.desc}}
</div>
</div>
</div>
</template>
<script>
export default {
data () {
return {
items: [
{
text: 'Foo',
desc: 'The Array.from() method creates a new Array instance from an array-like or iterable object.',
},
{
text: 'Bar',
desc: 'The Array.from() method creates a new Array instance from an array-like or iterable object.',
}
],
isActive: false
}
},
methods: {
toggle: function () {
this.isActive = !this.isActive;
}
},
}
</script>
You can add a property on each item in your list if description should be shown:
Alternatively, you can extract the
li
into a separate component.As @Nora said you can (and probably should) create a separate component for each list item, so you would have a component that accepts an
item
as a prop, then each component can have it's ownisActive
flag, which keeps the markup nice and clean:Component:
Markup
Now you can simply place the component inside your
v-for
:Here's the JSFiddle: https://jsfiddle.net/w10qx0dv/