I am making a voting application. I want to disable the button once clicking the voting button. How to disable the button.
template
  <v-btn
   v-for="choice in data.choices"
   @click="doVote(choice.id)"
   color="success"
   v-bind:key="choice.id">
   votes: {{ choice.votes }}
  </v-btn>
script
  data () {
    return {
      vote: null,
      questions: [],
    }
  },
  methods: {
    fetchData () {
      this.$request.questions.list().then(res => {
        this.questions = res.data.results
      })
    },
    // add votes
    doVote (vote) {
      if (!vote) {
        return
      }
      this.$request.questions.vote(vote).then(res => {
        this.fetchData()
      })
    },
  mounted () {
    this.fetchData()
  },
                The simplest thing do is set a variable when the voting button is pressed and then bind it to the buttons 'disabled' property:
v-bind:disabled="hasVoted"
                        v-btnhas a disabled property you can use; One way to do this could be create a clicked field to record all buttons you've clicked and check whether a specific button is in the clicked array:
<v-btn
   :disabled="clicked.includes(choice.id)"
   v-for="choice in data.choices"
   @click="doVote(choice.id)"
   color="success"
   v-bind:key="choice.id">
   votes: {{ choice.votes }}
</v-btn>
In data, initialize clicked as an empty array:
data () {
    return {
      vote: null,
      questions: [],
      clicked: []
    }
  }
Then in the doVote method, push the choice.id to clicked array when the event is fired:
doVote (vote) {
  this.clicked.push(vote)
  if (!vote) {
    return
  }
  this.$request.questions.vote(vote).then(res => {
    this.fetchData()
  })
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With