Reputation: 91
How can i toggle by btn specific div from list of items , which one i got from json and display using v-for ?
I am understand that i can`t just set v-if to div and add to vue data , but in this case it fill open and close all divs
<div class="about" v-if="list">
<div class="dat" v-for="data in item.items_situation" :key="data.a">
{{data.doc_valid_through}}
{{data.document_type}}
{{data.item_nr_full}}
{{data.item_text}}
</div>
<div class="situation-btn">
<input class="situatuin-btn" type="submit" value="Подбробнее" v-on:click="hide">
</div>
</div>
When i click the button i want to toggle one div not all
Upvotes: 3
Views: 2431
Reputation: 21
You can use v-bind:class to dynamically add class to elements. Code should be like this:
<div class="about" v-if="list">
<div v-for="data in items" v-bind:class="{hide: data.isHidden}" :key="data.key">
Something
</div>
<div class="situation-btn">
<input class="situatuin-btn" type="submit" value="Подбробнее" v-on:click="hide">
</div>
</div>
Then in hide
hide(){
this.items[i].isHidden = true
}
The data.isHidden
property decides whether an element in list should have hide
class.
See also Class and Style Bindings.
-------Edited-------
I have posted an example here
Upvotes: 0
Reputation: 2070
There are many ways to achieve this, I'll provide one for you.
data
by adding visible
property for every item.visible = false
by clicking it.visible === true
only)new Vue({
el: "#app",
data: {
items: [
{ text: "Learn JavaScript", id: 1 },
{ text: "Learn Vue", id: 2 },
{ text: "Play around in JSFiddle", id: 3 },
{ text: "Build something awesome", id: 4 }
].map(item => ({...item, visible: true})),
hiddenItems: []
},
computed: {
visibleItems() {
return this.items.filter(item => item.visible)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="dat" v-for="item in visibleItems" :key="item.id">
{{item.text}}
<button v-on:click.prevent="item.visible = false">X</button>
</div>
</div>
Upvotes: 3