elementUI中table列表中的tooltip内容换行

本文介绍了如何在表格中使用自定义slot和v-html指令处理后端返回的客户单号,避免show-overflow-tooltip的问题。通过v-if控制tooltip的显示,并在鼠标悬浮时动态调整显示状态,仅在超出时显示鼠标悬浮提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
这种情况下:show-overflow-tooltip是无法满足需求的。此时我们采取其他的方式。具体思路如下:

1.使用slot自定义el-tooltip内容
2.v-html指令转义,后端返回的数据使用正则处理,具体代码如下:

  <el-table-column prop="customer_order_no" label="客户单号" width="150">
                      <template slot-scope="scope">
                          <el-tooltip class="item" effect="dark" placement="top" 
                          <div v-html="ToBreak(scope.row.customer_order_no)" slot="content"></div>
                          <div class="oneLine">
                              {{scope.row.customer_order_no}}
                          </div>
                      </el-tooltip>
                      </template>
              </el-table-column>

因为我这边是想要直接在table中展示,所以让后端返回了,下面也就是匹配,换成换行标签。具体大家根据自己实际情况进行展示。

ToBreak (val) {
      return val.replaceAll(',', '<br />')
    },
.oneLine {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

注:删除 show-overflow-tooltip属性或设置为false,否则页面出现两个近乎重叠的tip

此时换行功能已经实现,但是 不管有没有超出,鼠标悬浮都会出现此效果。我们更倾向于超出的时候,有鼠标悬浮效果。通过变量控制。

<el-table-column prop="customer_order_no" label="客户单号" width="150">
                      <template slot-scope="scope">
                          <el-tooltip class="item" effect="dark" placement="top" v-if="!isShowTooltip">
                          <div v-html="ToBreak(scope.row.customer_order_no)" slot="content"></div>
                          <div class="oneLine" @mouseover="onMouseOver">
                              {{scope.row.customer_order_no}}
                          </div>
                      </el-tooltip>
                       <div class="oneLine"  @mouseover="onMouseOver" v-if="isShowTooltip">
                              {{scope.row.customer_order_no}}
                        </div>
                      </template>
              </el-table-column>
  data () {
    return {
      isShowTooltip: true,
    }
  },
onMouseOver (e) {
      var target = e.target
      let textLength = target.clientWidth
      let containerLength = target.scrollWidth
      if (textLength < containerLength) {
        // 溢出了
        this.isShowTooltip = false
      } else {
        this.isShowTooltip = true
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值