el-table的表头过长文字省略

自我摸索,如有BUG,欢迎指正修改。货不多说,直接源码。

<template>
  <div ref="textBoxRef" class="text-tips">
    <el-tooltip
      v-if="ellipsis"
      class="box-item"
      effect="dark"
      :content="$t(text)"
      placement="top-start">
      <span ref="textRef">{{ $t(text) }}</span>
    </el-tooltip>
    <span ref="textRef" v-else>{{ $t(text) }}</span>
  </div>
</template>

<script setup>
  import { ref, onMounted, nextTick } from 'vue'

  const props = defineProps({
    text: {
      type: String,
      required: true
    }
  })
  const textBoxRef = ref(null)
  const textRef = ref(null)
  const ellipsis = ref(false)
  onMounted(() => {
    textBoxRef.value.parentNode.style.display = 'flex';
    textBoxRef.value.parentNode.style.alignItems = 'center';
    textBoxRef.value.parentNode.style.justifyContent = 'center';
    // 绑定一个鼠标移入事件
    textBoxRef.value.parentNode.addEventListener('mouseenter', () => {
      if (textRef.value.scrollWidth > textBoxRef.value.clientWidth) {
        ellipsis.value = true
      } else {
        ellipsis.value = false
      }
    })
  })
</script>

<style lang="scss" scoped>
  .text-tips {
    max-width: calc(100% - 15px);
    display: inline-flex;
    justify-content: center;
    align-items: flex-start;
    overflow: hidden;

    span {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
</style>

 这是封装的组件,可以是直接引用的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值