0

so i have 3 buttons in a v-for loop with a click method in it.

I want to click on a button and disable the others, but also be able to click the active button and enable the other.

Thanks :)

'data': []

<div v-for="(index, value, key) in data.data">
    <button @click="onClick(index)">
        <div>{{ index.id }}</div>
    </button>
</div>

onClick(index) {//}
2
  • please provide a code example of what you have so far Commented Sep 22, 2018 at 19:26
  • @kfedorov91 done :) Commented Sep 22, 2018 at 19:36

1 Answer 1

1

Something like this?

var app2 = new Vue({
  el: "#app",
  data: {
    buttons: [false, false, false]
  },
  methods: {
    onClick(index) {
      if (this.buttons.every(b => !b)) {
        this.buttons = this.buttons.map((b, i) => (i === index ? false : true));
      } else {
        this.buttons = this.buttons.map(b => false);
      }
    }
  }
});

https://codepen.io/bart0810/pen/zJXNXx

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

3 Comments

But the data loading is not like I want to.
What do you mean, data loading?
buttons: [false,false,false] ... I get my data in a json object where I loop through with the v-for. In my v-for I have multiple buttons, depending on how much objects they are. So I dont know how I can implement the buttons array in my code some how

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.