1

I have a button in a v-for loop that when clicked retrieves some data. I then want to display that data below or in place of the button that was clicked.

<div v-for="(item, index) in items" :key="index">
  <button @click="fetchData(item.id)">Load Data</button>
  <ul v-if="THEDATALOADED">
    <li v-for="(data, index) in THEDATALOADED">
      {{ data.value }}
    </li>
  </ul>
</div>

I also want to retain all the data below each button that was clicked, so I can't just set this.THEDATALOADED = response.data, if I did, it would display that data in all the v-for iterations and update them all as well when any button is clicked.

1
  • I think that's a cool idea. If only one could use arrays. Commented Apr 3, 2020 at 20:15

1 Answer 1

2

I recommend that you push the fetched data in the correspondant item :

<div v-for="(item, index) in items" :key="index">
  <button @click="fetchData(item.id,index)">Load Data</button>
  <ul v-if="item.THEDATALOADED">
    <li v-for="(data, index) in item.THEDATALOADED">
      {{ data.value }}
    </li>
  </ul>
</div>

in the fetchData method :

 fetchData(id,index){
      ....
      let tmp=this.items[index];
      tmp.THEDATALOADED=response.data;
      this.$set(this.items,index,tmp);
      ...
  }

The following running code illustrates your use case, in this example i have a set of users, and each user has some posts that he created, when i click on the load Posts button it will fetch the posts of the correspondant user :

new Vue({
  el: '#app',
  data: {
    users: [],
  },
  mounted() {

    axios.get('https://jsonplaceholder.typicode.com/users')
      .then((response) => {
        this.users = response.data
      })
  },
  methods: {
    getPosts(id, index) {
      axios.get('https://jsonplaceholder.typicode.com/posts?userId=' + id)
        .then((response) => {
          let tmp = this.users[index];
          tmp.posts = response.data;
          this.$set(this.users, index, tmp);
        })
    }
  }
})
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="https://unpkg.com/[email protected]/dist/vue-axios.min.js"></script>
</head>

<body>
  <div id="app">
    <div v-for="(user, index) in users" :key="index">
      <span>{{user.name}}</span>
      <button @click="getPosts(user.id,index)">load Posts</button>
      <ul v-if="user.posts">
        <li v-for="(post, index) in user.posts">
          {{ post.title}}
        </li>
      </ul>
    </div>
  </div>
</body>

</html>

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

Comments

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.