简体   繁体   中英

How can I set b-popover target dynamically in vue js?

I want to show details of each state, Where user has clicked using one popover component. There all data will be set dynamically using state id.

Now my problem is, I can't set target dynamically. I want to set the popover target, where user has clicked.

I have tried this code below

<template>
  <div class="d-flex flex-column text-md-center">
    <div class="p-2">
      <b-btn id="texas" variant="primary" @click="onOpen">Details</b-btn>
      <b-btn id="california" variant="primary" @click="onOpen">Details</b-btn>
      <b-btn id="florida" variant="primary" @click="onOpen">Details</b-btn>
      <b-btn id="ohio" variant="primary" @click="onOpen">Details</b-btn>
    </div>

    <b-popover ref="popover" target="{{id}}" title="Popover">
      Hello <strong>{{id}}</strong>
    </b-popover>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        id:  ''
      }
    },
    methods: {
      onOpen(e) {
        this.id = e.target.id;
        this.$root.$emit('bv::show::popover',e.target.id);
      },
    }
  }
</script>

If these button ids from list you could use v-for on your b-popover

eg:

<template>
  <div class="d-flex flex-column text-md-center">
    <div class="p-2">
      <b-btn v-for="(eachButton, i) in ids" :key="i" :id="eachButton" variant="primary" @click="onOpen">Details</b-btn>
    </div>
    <b-popover ref="popover" v-for="(eachId, i) in ids" :key="i" :target="eachButton" title="Popover">
      Hello <strong>{{ eachId }}</strong>
    </b-popover>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        ids: [
          'texas',
          'california',
          'florida',
          'ohio',
        ]
        id:  ''
      }
    },
    methods: {
      onOpen(e) {
        this.id = e.target.id;
        this.$root.$emit('bv::show::popover',e.target.id);
      },
    }
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM