Display the <v-icon> component only when it is being hovered over in a Vuetify data table

In my Vuetify data table, I have headers and corresponding data fields. Within the props.item.name field, I have included a v-icon that should only be visible when hovering over that specific field. By default, the icon should not be displayed.

Below is the code snippet with both the script and HTML implementation.

For reference, here is the CodePen.

I would appreciate any assistance on how to achieve this functionality.

new Vue({
  el: '#app',
  data() {
    return {
      headers: [{
          text: 'Dessert (100g serving)',
          align: 'left',
          sortable: false,
          value: 'name'
        },
        {
          text: 'Calories',
          value: 'calories'
        },
        {
          text: 'Fat (g)',
          value: 'fat'
        },
        {
          text: 'Carbs (g)',
          value: 'carbs'
        },
        {
          text: 'Protein (g)',
          value: 'protein'
        },
        {
          text: 'Iron (%)',
          value: 'iron'
        }
      ],
      desserts: [{
          name: 'Frozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%'
        },
        {
          name: 'Ice cream sandwich',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%'
        },
        {
          name: 'Eclair',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%'
        },
        {
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%'
        },
      ]
    }
  }
})
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e3e0f0e1fcf3ecd5a4bba0bba4ad">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daacafbfaeb3bca39aebf4eff4ebe2">[email protected]</a>/dist/vuetify.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <v-app id="inspire">
    <v-data-table :headers="headers" :items="desserts" class="elevation-1">
      <template v-slot:items="props">
        <td>{{ props.item.name }}
        <v-icon right>cake</v-icon></td>
        <td>{{ props.item.calories }}</td>
        <td>{{ props.item.fat }}</td>
        <td>{{ props.item.carbs }}</td>
        <td>{{ props.item.protein }}</td>
        <td>{{ props.item.iron }}</td>
      </template>
    </v-data-table>
  </v-app>
</div>

Answer №1

Incorporate a data property named c_index (current index) and when the user hovers over the row, assign the index of the hovered row to c_index. Reset it to -1 when the mouse leaves the row:

<tr @mouseover="c_index=props.index" @mouseleave="c_index=-1">

Show the icon conditionally like this:

<v-icon right v-show="props.index==c_index">cake</v-icon>

Complete Demo

Answer №2

By incorporating a v-hover component within the v-slot:item.column_name of a data table, you have the ability to send a property through its v-slot and utilize it in this manner:

<template v-slot:item.action="{ item }">
    <v-hover v-slot:default="{ hover }">
        <v-badge
          :value="hover"          
          color="deep-purple accent-4"
          content="First Button"
          left
          transition="slide-x-transition"
         >
             <v-icon>mdi-fountain-pen-tip</v-icon>
         </v-badge>
    </v-hover>
</template>

You can also apply v-hover using CSS, but positioning adjustments may be necessary. See Full Demo

Answer №3

If you're looking for the optimal solution, consider using CSS

_ When hovering over v-icon:

.v-data-table .v-icon{visibility:hidden}
.v-data-table .v-icon:hover{visibility:visible}

_ When hovering over a td container

.v-data-table td .v-icon{visibility:hidden}
.v-data-table td:hover .v-icon:hover{visibility:visible}

This code will impact all instances of v-icon in your template. For more precision, assign a class to the specific v-icons you wish to hide.

...
<v-icon class="hidden" ... />
...

.hidden{visibility:hidden}
.hidden:hover{visibility:visible}

or

...
<td class="hidden" >
     <v-icon ...>
...
</td>
...

.hidden .v-icon{visibility:hidden}
.hidden:hover .v-icon{visibility:visible}

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

Display React component when clicked

As a newcomer to the world of React, I find myself intrigued by its potential. However, I am still in the process of grasping the fundamental concepts and would greatly appreciate any explanations provided. My goal is to display an 'About' compo ...

The $http service factory in Angular is causing a duplication of calls

After creating an angular factory that utilizes the $http service, I encountered a problem where the HTTP request is being made twice when checking the network tab in the browser. Factory: app.factory('myService', function ($http, $q) { var de ...

Having trouble retrieving cookie in route.ts with NextJS

Recently, I encountered an issue while using the NextJS App Router. When attempting to retrieve the token from cookies in my api route, it seems to return nothing. /app/api/profile/route.ts import { NextResponse } from "next/server"; import { co ...

Transferring an object from one inventory to another

I'm in the process of developing a task manager that enables users to add and remove tasks. I am also working on enabling the ability for users to transfer tasks from one list to another. The current code I have written doesn't seem to be functio ...

Encountering an ongoing problem with trial repetition in JsPsych, which is causing the looping to continue endlessly without

As a beginner in JsPsych, I'm diving into creating a basic math quiz task to sharpen my skills. The goal is to generate random math questions as prompts and stop the task after 10 correct answers. I've managed to code that selects random math pro ...

Transferring data to a child component through Route parameters

Although I have come across numerous questions and answers related to my query, I still seem unable to implement the solutions correctly. Every time I try, I end up with an 'undefined' error in my props. Let's take a look at my parent compo ...

A lateral sliding motion

Here is the HTML code I am working with: <div class="menuTabs"> <div class="mtabArrowLeft">Left</div> <input class="menutabBTN" name="" type="button" value="a" /> <input class="menutabBTN" name="" type="b ...

Showing the precise age in years by utilizing the provided 'Date of Birth' selected from the datePicker

Is it possible to retrieve the date entered in the datePicker and use it to populate the current age field in a PHP file using either PHP or Javascript? if ($key == 'currentage') { $group[] = $this->form->createEleme ...

Having trouble establishing a connection with SignalR on my backend, it just doesn't seem to work properly

I am currently working on a project where I am using Vue with TypeScript and @microsoft/signalr. My goal is to create a chat feature using SignalR, and on the backend, I am utilizing C# with ASP.NET Core and docker. Here is the code snippet I have impleme ...

What is the most efficient way to update a particular item in an array within a React component's state while maintaining its conventional approach?

When working with React component state, I often struggle with manipulating a specific item in an array. Take this example: state={ menus:[ { id:1, title: 'something', 'subtitle': 'another something', switch ...

Utilizing jQuery to toggle containers based on link clicks

Hello everyone, I'm having trouble getting this code to work. I have a set of 4 links and I need to display one div container for 3 of them, and another for the remaining 1 link, switching back and forth. Any suggestions? <div class="content activ ...

Is there a way to change the labels of checkboxes within a table that is created dynamically?

I am new to JavaScript/jQuery and facing an issue with modifying checkbox labels in a dynamically generated table. <td> <input type="checkbox> <b>$18,000.00</b> ($18000+) Diamond Sponsor<br> <input type="checkbox> <b ...

The appearance of the circle in Safari is rough and lacks smoothness

My circle animation works perfectly on Google Chrome, but when I switch to Safari the edges appear faded and blurry. I tried adding "webkit" to fix it, but had no luck. Is there a compatibility issue with Safari and CSS animations? Here is my code: Snapsh ...

Create a bar chart utilizing Highcharts drilldown feature by integrating data from two distinct JSON endpoints

I am currently working with two different JSON endpoints in order to create a Highcharts bar graph with drilldown functionality. The initial data for the graph is fetched dynamically from one endpoint and upon clicking on a bar, the graph will drill down t ...

Having trouble with making a POST request in Node.js using Express.js and Body-parser?

Just starting out with nodejs and currently in the learning phase. I've encountered an issue where my POST request is not functioning as expected. I set up a basic html form to practice using both GET and POST requests. Surprisingly, the GET request w ...

React Error: Unable to iterate over this.state.descriptions

Currently facing an issue while trying to resolve this error https://i.stack.imgur.com/BZ304.png The goal is to generate an automated form using the following function: let descriptionsForm = ( <form> {this.state.descriptions.map((d ...

Add the element to a fresh array only if there are no duplicate values present

I am faced with a challenge involving an array of names where duplicate entries, such as "Kenny," are causing some confusion. I only want each name to be included in the new array once, but I'm struggling to achieve this. Here's my progress so fa ...

I'm caught in a never-ending cycle as I navigate through sending information to my Python API and subsequently utilizing that information in the Next.js application to execute the Python script

I am encountering an issue with the response message, getting "Error sending data." The problem seems to arise when trying to retrieve data in server.py that is already specified in the code. For example, instead of using "msg_user", I should use ...

How can I combine these scripts that are not working simultaneously?

I have two scripts on my site that are based on the meta title, and I'm trying to make them work together. I thought changing the function names would be enough, but when I use both scripts, one doesn't work. Why is this happening? Also, should I ...

Validating forms in ReactJS

I have developed a basic form validation feature for React. The inspiration for this project came from the following source: When I try to submit the form, input errors arise within the isValid function. I am seeking assistance in resolving this issue. A ...