3

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

1
  • share code snippet ? Commented Jun 13, 2019 at 9:55

2 Answers 2

3

There are many ways to achieve this, I'll provide one for you.

  1. Map your JSON array in data by adding visible property for every item.
  2. Add button inside loop (so its attached for every item) and make visible = false by clicking it.
  3. Display filtered results (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>

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

How it supposed to call exactly for instance second div ? When should i define [i] , can you describe a little bit more ?
@Bravis Well i is just the index of element in list to be hidden, so if you want the second to hide you just need to change it to this.data.items[1].isHidden = true. And as there is no style applied to class hide you'll need to write CSS for this class yourself like .hide{ display: none; }.
i dont know , it does`not work , event this data.isHidden , how it supposed to work ? i should have this data in my Json , i dont
Sorry i made a mistake and i have edited my answer. It should be this.items[i].isHidden not this.data.items[i].isHidden

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.