vue3.0+antd实现table内容超出自动tooltip

Tooltip实现

<template>
  <a-tooltip
    v-if="isShowTooltip"
    placement="top"
    trigger="hover"
    overlayClassName="table-class"
    v-model:visible="isShow"
  >
    <template #title>
      <span>{{ content }}</span>
    </template>
    <div class="content" @mouseleave="mouseleave">
      <span ref="contentRef">
        <slot></slot>
      </span>
    </div>
  </a-tooltip>
  <div v-else class="content" @mouseenter="mouseenter">
    <span ref="contentRef">
      <slot>{{ contentText }}</slot>
    </span>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";

export default defineComponent({
  props: {
    content: {
      type: String,
      default: "",
    },
  },
  setup() {
    const isShow = ref(true);
    const contentRef = ref();
    const isShowTooltip = ref(false);
    const mouseleave = () => {
      // isShowTooltip.value = false
    };
    const mouseenter = () => {
    // 关键代码逻辑
      if (
        contentRef.value.parentNode.offsetWidth > contentRef.value.offsetWidth
      ) {
        isShowTooltip.value = false;
      } else {
        isShowTooltip.value = true;
      }
    };
    return {
      isShow,
      contentRef,
      mouseenter,
      mouseleave,
      isShowTooltip,
    };
  },
});
</script>

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

Table封装及tooltip使用

主要看tooltip的功能实现

<template>
  <a-table v-bind="$attrs" :rowKey="rowkeyIndex" :pagination="pagination">
    <template #bodyCell="{ column, text, record }">
      <template v-if="column.slot">
        <!-- 如果配置了ellipsis -->
        <ele-tooltip v-if="column.ellipsis" :content="text">
          <slot
            :name="column.dataIndex"
            :col="column"
            :rowItem="column"
            :text="text"
            :rowData="record"
          ></slot>
        </ele-tooltip>
        <slot
          v-else
          :name="column.dataIndex"
          :col="column"
          :rowItem="column"
          :text="text"
          :rowData="record"
        ></slot>
      </template>
      <!-- 默认没有slot 且配置了ellipsis -->
      <template v-else-if="column.ellipsis">
        <ele-tooltip :content="text">
          {{ text }}
        </ele-tooltip>
      </template>
    </template>
    <template #headerCell v-if="slot.header">
      <slot name="header"></slot>
    </template>
  </a-table>
</template>

<script lang="ts">
import { defineComponent, computed } from "vue";
import eleTooltip from "@/components/tooltip/index.vue";
export default defineComponent({
  name: "commonTable",
  components: {
    eleTooltip,
  },
  props: {
    keyId: {
      type: String,
      default: "",
    },
    showQuickJumper: {
      type: Boolean,
      default: true,
    },
    showLessItems: {
      type: Boolean,
      default: false,
    },
    showSizeChanger: {
      type: Boolean,
      default: true,
    },
    total: {
      type: Number,
      default: 0,
    },
    paginationSize: {
      type: String,
      default: "",
    },
    defaultPageSize: {
      type: Number,
      default: 10,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    current: {
      type: Number,
      default: 1,
    },
  },
  setup(props, context) {
    const attrs: any = context.attrs;
    const slot = context.slots;
    const columnsCustom = computed(() => {
      return (
        attrs.columns &&
        attrs.columns
          .filter((item: any) => {
            return item.slot;
          })
          .map((item: any) => item.slot)
      );
    });
    // 重新定义表格分页
    const pagination = computed(() => {
      return {
        size: props.paginationSize,
        hideOnSinglePage: false,
        pageSize: props.pageSize,
        current: props.current,
        defaultPageSize: props.defaultPageSize,
        showQuickJumper: props.showQuickJumper,
        showLessItems: props.showLessItems,
        showSizeChanger: props.showSizeChanger,
        pageSizeOptions: ["10", "20", "50", "100"],
        total: props.total,
        showTotal: (total: number) => {
          return `${total}`;
        },
      };
    });
    const rowkeyIndex = (row: any) => {
      return row.id || row[props.keyId];
    };
    return {
      slot,
      rowkeyIndex,
      columnsCustom,
      pagination,
    };
  },
});
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝对零度HCL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值