Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify tooltip in data table

I want to add some tool tip in a vuetify datatable , but nothing happen.

this is the template that I need to use a tool tip

<template v-slot:item.state="{ item }">
   <div :class="lights(item)"></div>
</template>

and I use it like this

<v-tooltip left>
      <template v-slot:activator="{ on }">
         <template v-slot:item.state="{ item }">
           <div :class="lights(item)"></div>
         </template>
      </template>
   <span>Tooltip</span>
</v-tooltip>

and I have also an modal who need a tool tip but it have a v-slot activatior too :

<template v-slot:activator="{ toggle }">
   <v-tooltip bottom>
       <template v-slot:activator="{ on }" >
            <v-icon :color="color" v-on="on" v-on="toggle">mdi-power</v-icon>
        </template>
         <span>Tooltip</span>
    </v-tooltip>
</template>

How can I do ?

like image 981
TheDevGuy Avatar asked Dec 03 '19 16:12

TheDevGuy


1 Answers

You are close. The structure is a bit off and you're missing a key feature.

Refactor to the following structure:

<template v-slot:item.state="{ item }">
  <v-tooltip left>
    <template v-slot:activator="{ on }">
      <div :class="lights(item)" v-on="on"></div>
    </template>
    <span>Tooltip: {{ item.yourKeyHere }}</span>
  </v-tooltip>
</template>

You need to activate the hover mechanism via on so in the div[class="lights(item)"] template of the row td you need to set v-on="on".

Below is a working example modified from the Vuetify docs v-data-table example for customizing default rows (alt)

Edit: Add nested components (dialog with a tooltip) using same default activator template syntax. (Click on the 'Fat' value in a row, then hover over dialog text to display tooltip)

Vue.config.productionTip = false;
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  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%',
        },
        {
          name: 'Gingerbread',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: '16%',
        },
        {
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: '0%',
        },
        {
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: '2%',
        },
        {
          name: 'Honeycomb',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: '45%',
        },
        {
          name: 'Donut',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: '22%',
        },
        {
          name: 'KitKat',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: '6%',
        },
      ],
    }
  },
})
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

<div id="app">
  <v-app>
    <v-data-table
      :headers="headers"
      :items="desserts"
      :items-per-page="5"
      class="elevation-1"
    >
      <template v-slot:item.calories="{ item }">
        <v-tooltip left>
          <template v-slot:activator="{ on }">
            <v-chip dark v-on="on">{{ item.calories }}</v-chip>
           </template>
           <span>{{ item.name }}</span>
         </v-tooltip>
      </template>
      <!-- Edit to show independant nested activators -->
      <template v-slot:item.fat="{ item }">
        <v-dialog width="16rem">
          <template v-slot:activator="{ on }">
            <td v-on="on" style="cursor: pointer">{{ item.fat }}</td>
           </template>
           <v-card>
             <v-tooltip top color="primary">
               <template v-slot:activator="{ on }">
             <v-card-title class="d-flex justify-center" v-on="on">{{ item.name }}</v-card-title>
               </template>
               <span>Fat: {{ item.fat }}</span>
             </v-tooltip>
          </v-card>
         </v-dialog>
      </template>
    </v-data-table>
  </v-app>
</div>
like image 114
seantunwin Avatar answered Sep 20 '22 04:09

seantunwin