[ant-design]动态ToolTip,table列表根据文字宽度表格宽度动态显示,附代码

需求背景:在table列表中ant-design的tooltip需要根据文字多少来动态显示/隐藏,不会超出的不显示tooltip

www.baidu.com文字不超出,鼠标移入不显示tooltip 


组件代码


<script>
import {
  defineComponent,
  getCurrentInstance,
  nextTick,
  ref
} from 'vue';

export default defineComponent({
  props: {
    text: {
      default: ''
    }
  },
  setup() {
    const showTooltip = ref(false);
    const textRef = ref();
    const { proxy } = getCurrentInstance();

    const setTooltip = () => {
      nextTick(() => {
        const outElWidth = proxy.$el && proxy.$el.offsetWidth;
        const textElWidth = textRef.value && textRef.value.offsetWidth;

        if (outElWidth < textElWidth) {
          showTooltip.value = true;
        } else {
          showTooltip.value = false;
        }
      });
    };

    const resize = () => {
      setTooltip();
    };

    return {
      textRef,
      showTooltip,
      setTooltip,
      resize
    };
  },
  render() {
    return (
      <div vResize={this.resize}>
        {this.showTooltip ? (
          <ATooltip placement="top" arrowPointAtCenter={true}>
            {{
              title: () => <span>{this.text}</span>,
              default: () => (
                <div class="out">
                  <span ref="textRef" class="text">
                    {this.text}
                  </span>
                </div>
              )
            }}
          </ATooltip>
        ) : (
          <div class="out">
            <span ref="textRef" class="text">
              {this.text}
            </span>
          </div>
        )}
      </div>
    );
  }
});
</script>
<style scoped lang="less">
.out {
  word-break: break-word;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  line-height: 18px;
}
</style>
  app.directive("resize", {
    mounted(el: HTMLElement | any, binding: any): void {
      let width = "",
        height = "";
      function isReize() {
        const defaultView: any = document.defaultView;
        const style = defaultView.getComputedStyle(el);
        if (width !== style.width || height !== style.height) {
          binding.value();
        }
        width = style.width;
        height = style.height;
      }
      el.__vueSetInterval__ = setInterval(isReize, 300);
    },
    unmounted(el: HTMLElement | any) {
      clearInterval(el.__vueSetInterval__);
    },
  });

Table里使用

        <template #bodyCell="{ column, record, text }">
            <TooltipCell :text="text" />
        </template>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值