Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
How to change the two switch data with click with Vuejs

Please can someone teach me the method to swap two different data on click

For example, we have a data

data() {
        return {

          data_one: [
            {
              name: "Simo",
              age: "32"
            }
          ],

          data_two: [
            {
              name: "Lisa",
              age: "25"
            }
          ],

        }
      },

then I want to put this data on v-for, and I want to add two buttons to swap from data_one and data_two and display data_one as default.

<button @click="change_to_data_one">Data 1<button>
<button @click="change_to_data_two">Data 2<button>

<li v-for="item in data_one">{{item.name}} - {{item.age}}<li>

๐Ÿ™๐Ÿ™๐Ÿ™๐Ÿ™๐Ÿ™

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Try to use computed property:

new Vue({
  el: "#demo",
  data() {
    return {
      data_one: [{name: "Simo", age: "32"}],
      data_two: [{name: "Lisa", age: "25"}],
      selected: "data_one"
    }
  },
  computed: {
    showData(){
      return this[this.selected]
    }
  },
  methods: {
    swap(val) {
      this.selected = val
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <button @click="swap('data_one')">Data 1</button>
  <button @click="swap('data_two')">Data 2</button>
  <ul>
    <li v-for="(item, i) in showData" :key="i">{{item.name}} - {{item.age}}</li>
  </ul>
</div>

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!