Implementing Vue.js Popover for Individual Dropdown Items: A How-To Guide

I need unique information text to display for each dropdown item when hovered. I am using bootstrap-vue and have a loop set up for the dropdown items.

<b-dropdown :text="currentColorType">
    <b-dropdown-item v-for="colorType in colorTypes"
    :key="colorType.id"
    :value="colorType.name">{{colorType.name}}</b-dropdown-item>
</b-dropdown>
<b-popover target="id" placement="bottom" triggers="hover blur">
    <div>Unique Information Text Here</div>
</b-popover>

Is there a way to dynamically attach an id as target in the popover? I tried options like:

id="colorType.name"
id="`${colorType.name}`"

but they did not work. I'm also unsure if the popover would even trigger with this setup. Any suggestions on a better approach to show info on hover for dropdown items?

Answer №1

If you want to simplify your code, consider using the v-b-popover directive instead of <b-popover>. This way, you can easily connect the popover directly to <b-dropdown-item>.

new Vue({
  el: '#app',
  data() {
    return {
      colorTypes: [{
          id: 1,
          name: 'Blue'
        },
        {
          id: 2,
          name: 'Red'
        },
        {
          id: 3,
          name: 'Green'
        }
      ]
    }
  }
})
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="294b46465d5a5d5b4859691d071c071b">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b0bdbda6a1a6a0b3a2ffa4a7b792e0fce3e4fce2">[email protected]</a>/dist/bootstrap-vue.css" rel="stylesheet" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3f3232292e292f3c2d702b28381d6f736c6b736d">[email protected]</a>/dist/bootstrap-vue.js"></script>

<div id="app">
  <b-dropdown text="Colors">
    <b-dropdown-item v-for="colorType in colorTypes" :key="colorType.id" :value="colorType.name" v-b-popover.hover="colorType.name">
      {{ colorType.name }}
    </b-dropdown-item>
  </b-dropdown>
</div>

Example with HTML inside the popover

new Vue({
  el: '#app',
  data() {
    return {
      colorTypes: [{
          id: 1,
          color: 'Blue',
          name: '<b class="text-primary">Blue<b>'
        },
        {
          id: 2,
          color: 'Red',
          name: '<b class="text-danger">Red<b>'
        },
        {
          id: 3,
          color: 'Green',
          name: '<b class="text-success">Green<b>'
        }
      ]
    }
  }
})
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="accec3c3d8dfd8decddcec988299829e">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8defe2e2f9fef9ffecfda0fbf8e8cdbfa3bcbba3bd">[email protected]</a>/dist/bootstrap-vue.css" rel="stylesheet" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6964647f787f796a7b267d7e6e4b39253a3d253b">[email protected]</a>/dist/bootstrap-vue.js"></script>

<div id="app">
  <h5>Using the HTML modifier</h5>
  <b-dropdown text="Colors">
    <b-dropdown-item v-for="colorType in colorTypes" :key="colorType.id" :value="colorType.name" v-b-popover.hover.html="colorType.name">
      {{ colorType.color }}
    </b-dropdown-item>
  </b-dropdown>
  <hr />
  <h5>Using an object</h5>
  <b-dropdown text="Colors">
    <b-dropdown-item v-for="colorType in colorTypes" :key="colorType.id" :value="colorType.name" v-b-popover.hover="{ content: colorType.name, html: true }">
      {{ colorType.color }}
    </b-dropdown-item>
  </b-dropdown>
</div>

Answer №2

To properly connect an element to another, consider binding a specific property to the

id attribute within the <b-dropdown> component.

Additionally, make sure to utilize the v-for directive when working with the <b-popover> as well.

For example:

<b-dropdown :value="selectedItem">
  <b-dropdown-item 
   v-for="item in items"
   :key="item.id"
   :id="item.id"
   :value="item.name"
  >
    {{ item.name }}
  </b-dropdown-item>
</b-dropdown>

<b-popover 
 v-for="(type, index) in types" 
 :key="index" 
 :target="type.id" 
 placement="top" 
 triggers="hover click"
>
  <div>Additional information will go here</div>
</b-popover>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Unlocking the Power of mediaElementjs: Easy Steps to Accessing the Player Instance

I'm facing a small issue with the MediaElement.js player. To access the player instance, I usually use the following code (which works in HTML5 compatible browsers): // Retrieve player this.playerId = $('div#shotlist-player video').att ...

Retrieve JSON information by utilizing variable string interpolation

Why is it that when I try to access an integer stored under id's/keys in a JSON object, the code doesn't work on its own? var rooms = { 'kitchen': [7, 40, 36, 16], 'livingroom': 31, 'livingroom2': 15, ...

Bing Translator and XMLHttpRequest are two powerful tools for translating and

When running the code snippet below, I encounter an issue where I am not receiving status 200 and responseText. However, when using the following URL: http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate?appId=F1B50AB0743B541AA8C070890 ...

Output various strings to the standard output and stream them individually

Is there a way to redirect each string output to standard out to another command? // Example file: example.js #!/usr/bin/env node process.stdout.write('foo') process.stdout.write('bar') After running ./example.js | wc -m, the total ch ...

What could be causing the issue with the $http.delete method in AngularJS?

When trying to use $http.delete with Django, I encountered an HTTP 403 error. Here is my JS file: var myApp = angular.module('myApp',['ui.bootstrap']); myApp.run(function($http) { $http.defaults.headers.post['X-CSR ...

Retrieving objects from JSON data

I'm facing a challenge in creating a React component that showcases test results from various courses. The issue lies in accessing the scores object within the provided JSON file. Here is an excerpt from the JSON data I am currently working on: [ ...

What is the best way to expand a Modal in Bootstrap 4?

Initially, I am facing an issue with Object-Oriented Programming in javascript, and my understanding of it is quite limited. However, I urgently need to resolve this problem... Currently, I am attempting to utilize a library designed for Bootstrap 3 withi ...

Step-by-step guide on transforming jQuery code into Vue JS:

Recently delving into Vue, attempting to translate previous JS + JQuery code into Vue Here is the list I'm working with: <ul id="progressbar"> <li class="active">integration Ip's</li> <li>T ...

Whenever I use NextJS's <Link> component, I always end up getting redirected to a

After searching online, I came across this question and tried to implement the suggested solution, but it's still not working for me. Apologies for any duplication. I have a simple link tag that is resulting in a 404 error: <Link className={classe ...

What could be causing my NextAuthJS discord login to function in the test environment but fail to deploy in production on NextJS 13?

After attempting to deploy my NextJS application on Vercel, I encountered a problem with the AuthJS sign-in model. While running the program using npm run dev, everything works smoothly and I can log in to my Discord account linked to the developer portal& ...

Steps for changing the language in KeyboardDatePicker material ui

Currently, I am utilizing material ui on my website and leveraging the KeyboardDatePicker API with successful results. The only issue is that the months' names and button text are displayed in English, whereas I would prefer them to be in Spanish. Des ...

Updating columns in MongoDB using arrays in JavaScript has just gotten easier

When trying to update a mongoDB collection with an array value, the update fails without any error message. This method causes the issue: var arr = ["test","test1","test2"]; $.ajax('http://my.mongodb.com/collection?id=80a2c727de877ac9' , { ...

The functionality of item.classList.toggle() in HTML+CSS+JS fails to execute

I have been working on a program that aims to flip a card when it is clicked. The JavaScript code I have written for this functionality is as follows: /* Card flipping onclick */ import "./Stylesheets/FlipCardStyle.css" var cards = document.quer ...

Tips on how to automatically rearrange a jQuery UI sortable list

Currently, I am working with 2 jQuery UI sortable lists which can be found at http://jqueryui.com/demos/sortable/#connect-lists. The process involves dragging items from list A (catalog) to list B (basket). I have a specific requirement where I need the ...

Are HTML entities ineffective in innerHTML in javascript?

Take this example: <script type="text/javascript> function showText() { var text = document.getElementById("text-input").value; document.getElementById("display").innerHTML = text; } </script> <?php $text = "<html>some ...

How to Utilize Vuex Module in a JavaScript File

I am currently utilizing the Quasar framework with Vue3 and have imported a module into my Vuex Store. It functions properly when utilized in a Vue file, but how can I access it from a JS file? I am able to log the states in the settings module, however th ...

Detecting whether a browser is capable of supporting dark mode

One method to determine if dark mode is active is by using prefers-color-scheme: dark: const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; Is there a way to detect if a browser supports dark mode as well? (By "supports ...

The Safari browser showcases the dropdown area in a unique way

Is there a way to make the dropdown area size consistent across different browsers like Chrome, Firefox, and Internet Explorer? Here is the code snippet: <div id="category-dropdown" style="float:left; display:inline-block; padding:8px 0px 0px 5px; dis ...

Accessing an array within a function from a separate .JS file

Currently, I am facing a somewhat awkward predicament where I have a function in main.js that requires an array that is populated in second.js... In simple terms, the function in main.js is designed to be reusable: function chug() { p1.innerHTML = st ...

How about utilizing node.js as a peer for WebRTC?

Are there any available modules for utilizing node.js as a peer in WebRTC? I am interested in using WebRTC in a client/server manner rather than P2P for its ability to send packets unreliably (i.e. avoiding the delay caused by TCP's guarantee of packe ...