Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to hang click on only one element using v-for?

Hi I’m new to vue and I’m trying to complete one task. I have dynamic component toggles which I render with v-for. Can you suggest how can I pass an extra click to only one button (button ‘border-left’)? Desirable illustrative examples

<script>
        import Vue from "vue";
        import BorderLeftComonent from "./BorderLeftComonent.vue";
        import TextBalloonComponent from "./TextBalloonComponent.vue";
        import DashedComponent from "./DashedComponent.vue";
        export default Vue.extend({
    data() {
        return {
            component: "button[0].name",
            color: "",
            buttons: [
              {
                label: "A",
                isActive: false,
                type: "border-left",
                name: "BorderLeftComonent",
              },
              {
                label: "A",
                isActive: false,
                type: "text-balloon",
                name: "TextBalloonComponent"
              },
              {
                label: "A",
                isActive: false,
                type: "dashed",
                name: "DashedComponent"
              },
        ],
        };
    },
    methods: {
        toggleShowPopup() {
            this.isOpen = !this.isOpen;
        },
        activeBtn(event, index) {
          this.buttons[index].isActive = !this.buttons[index].isActive;
    }
    },

    computed: {
      currentComponent() {
        return this.component;
      },
      cssVars() {
        return {
          '--border-left': this.color,
        }
      }
    },
</script>

template is presented here

<template>
      <div id="btn-box">
        <button
          v-for="(button, index) in buttons"
          :key="index"
          :class="button.isActive ? 'on' : 'off'"
          @click="component = button.name, activeBtn($event, index)">
        <div :class="`btn btn-${button.type}`">{{ button.label }}</div>
      </button>
      </div>
</template>

the method i need to pass to only one button

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

toggleShowPopup() {
            this.isOpen = !this.isOpen;
        }

>Solution :

You need conditional event binding. Try this:

<button
   v-for="(button, index) in buttons"
   :key="index"
   :class="button.isActive ? 'on' : 'off'"
   @click="component = button.name, activeBtn($event, index), button.type === 'border-left' && toggleShowPopup()">
   <div :class="`btn btn-${button.type}`">{{ button.label }}</div>
</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading