Search code examples
vue.jsquasar

Looking for a way to show tooltip only when the element is truncated


I need help finding a solution to show the tooltip element only when the span element is truncated.

   <q-td v-for="(col,index) in props.cols" :key="col.name" :props="props">
        <span class="truncate">{{ col.value }}</span>
        <q-tooltip content-class="bg-primary" anchor="top middle" self="top middle" v-if="(index===0 || index===4) && col.value">{{ col.value }}</q-tooltip>
    </q-td>

I tried to do it with scrollWidth and clientWidth but when I try to access the span element with document.getElementById(elementID) it returns undefined, I assume because the span element is dynamically added. The truncate class is defined like this:

.truncate {
    display: block;
    width: 0;
    min-width: 100%;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

Solution

  • You can create a method to check your text length, and display tooltip only when text length is bigger than a particular threshold (hence it was truncated). For example:

    <q-td v-for="(col,index) in props.cols" :key="col.name">
        <span class="truncate">{{ col.value }}</span>
        <q-tooltip content-class="bg-primary" v-if="isTooltip(col.value)"> 
           {{ col.value }}
        </q-tooltip>
    </q-td>
    
    methods: { 
      isTooltip(col) {
        return col.length > 10
      }
    }