在 Vuetify 数据表中仅显示悬停的 <v-icon>

Tre*_*ick 2 javascript vue.js vue-component vuejs2 vuetify.js

我有一个 Vuetify 数据表,分别带有标题和数据字段,在 props.item.name 我添加了一个 v 图标,但我只想在我将鼠标悬停在相应字段上时显示该图标。默认情况下它不应该出现。

我已经添加了下面的代码和脚本和 HTML。

这是一支笔

关于如何实现这一目标的任何帮助将不胜感激。

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%'
        },
      ]
    }
  }
})
Run Code Online (Sandbox Code Playgroud)
<link href="https://cdn.jsdelivr.net/npm/vuetify@1.5.18/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.5.18/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>
Run Code Online (Sandbox Code Playgroud)

Bou*_*him 5

添加一个名为例如c_index(当前索引)的数据属性 ,当您将鼠标悬停在行上时,您将悬停的索引分配给c_index并在鼠标离开时将其重置为 -1:

  <tr @mouseover="c_index=props.index" @mouseleave="c_index=-1">
Run Code Online (Sandbox Code Playgroud)

并有条件地显示该图标,例如:

  <v-icon right v-show="props.index==c_index">cake</v-icon>
Run Code Online (Sandbox Code Playgroud)

Full Demo