element ui Table 文字超出3行 用...省略号表示,并在toolti

文章讲述了如何在HTML表格中处理过长的文字,通过CSS实现最多展示3行并用省略号表示超出部分,同时在鼠标移入时利用JavaScript控制tooltip显示完整内容。方法涉及到文本宽度计算和行数限制。

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

需求:table文字过长,最多展示3行,超出用...省略号显示,鼠标移入时用tooltip文字提示,文字没有超出3行则不用tooltip。

html:

            <el-table-column label="政策" prop="policyAccording" class="hide" width="170">
              <template #default="scope" >
                <el-tooltip :content="scope.row.policyAccording" effect="light" placement="top-start" :disabled="!isShowTooltip" >
                  <div class="hide" @mouseover="onMouseOver($event,scope.row.policyAccording)">
                    <span :ref="scope.row.policyAccording">{{ scope.row.policyAccording || '-' }}</span>
                  </div>
                </el-tooltip>
              </template>
            </el-table-column>

css:

  .hide {
    display: -webkit-box;
    text-overflow: ellipsis;
    overflow: hidden;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    white-space: pre-line;
  }

js:

    onMouseOver (obj, str) { 
      // 内容超出,显示文字提示内容
      /*currentWidth 为文本在页面中所占的宽度,创建标签,加入到页面,获取currentWidth ,最后在移除*/
      let TemporaryTag = document.createElement('span');
      TemporaryTag.innerText = str;
      TemporaryTag.className = 'getTextWidth';
      document.querySelector('body').appendChild(TemporaryTag);
      let currentWidth = document.querySelector('.getTextWidth').offsetWidth;
      document.querySelector('.getTextWidth').remove();

      /*cellWidth为表格容器的宽度*/
      const cellWidth = obj.target.offsetWidth

      /*当文本宽度小于||等于容器宽度3倍时,代表文本显示未超过3行*/
      currentWidth <= (3*cellWidth) ? this.isShowTooltip = false : this.isShowTooltip = true
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值