[el-tag]使用多个el-tag,自动判断内容是否超出

项目中有需求显示el-tag标签 内容超出需要显示省略号并且点击省略显示剩余标签

复制代码
<template>
  <div ref="tagContainer" class="tag-list">
    <!-- 显示的标签 -->
    <el-tag
      v-for="(tag, index) in visibleTags"
      :key="index"
      class="tag-item"
      @click.stop
    >
      {{ tag }}
    </el-tag>

    <!-- 如果有更多标签,则显示省略按钮和 Popover -->
    <el-popover
      v-if="showPopover"
      placement="top"
      trigger="click"
      width="300px"
    >
      <template #reference>
        <el-tag class="more-tag"><moreFilled /></el-tag>
      </template>
      <div class="flex flex-wrap gap-[4px] w-full">
        <el-tag
          v-for="(tag, index) in hiddenTags"
          :key="index"
          class="popover-tag"
        >
          {{ tag }}
        </el-tag>
      </div>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, onUnmounted } from "vue";
import moreFilled from "~icons/ep/more-filled";

// 定义 Props
const props = defineProps({
  tags: {
    type: Array<any>,
    default: () => [] as any[]
  }
});

// DOM 引用
const tagContainer = ref<HTMLDivElement | null>(null);

// 是否显示 Popover
const showPopover = ref(false);

// 可见标签和隐藏标签
const visibleTags = ref<string[]>([]);
const hiddenTags = ref<string[]>([]);

// 计算可见标签和隐藏标签
const calculateTags = () => {
  if (!tagContainer.value) return;

  const containerWidth = tagContainer.value.offsetWidth;

  let totalWidth = 0;

  visibleTags.value = [];
  hiddenTags.value = [];

  props.tags.forEach(tag => {
    const tempSpan = document.createElement("span");
    tempSpan.style.position = "absolute";
    tempSpan.style.visibility = "hidden";
    tempSpan.style.whiteSpace = "nowrap";
    tempSpan.innerText = tag;
    document.body.appendChild(tempSpan);
    const tagWidth = tempSpan.offsetWidth + 22; // 加上一些 padding  gap-4 和tag内置的padding9 9 + 9 + 4
    document.body.removeChild(tempSpan);
    if (totalWidth + tagWidth <= containerWidth) {
      visibleTags.value.push(tag);
      totalWidth += tagWidth;
    } else {
      hiddenTags.value.push(tag);
    }
  });

  showPopover.value = hiddenTags.value.length > 0;
};

// 监听窗口大小变化重新计算
onMounted(() => {
  calculateTags();
  window.addEventListener("resize", calculateTags);
});

// 组件卸载时移除事件监听
onUnmounted(() => {
  window.removeEventListener("resize", calculateTags);
});
</script>

<style lang="scss" scoped>
.tag-list {
  display: flex;
  gap: 4px;
  align-items: center;
  overflow: hidden;
}

.tag-item {
  white-space: nowrap;
}

.more-tag {
  cursor: pointer;
}

.popover-tag {
  max-width: 100%; // 设置最大宽度,防止无限延伸
  height: auto;
  line-height: 24px;
  word-break: break-all; // 强制单词内换行
  white-space: normal; // 允许换行
}
</style>
相关推荐
ccccc__1 天前
基于vue3完成领域模型架构建设
前端
Cherry的跨界思维1 天前
【AI测试全栈:Vue核心】19、Vue3+ECharts实战:构建AI测试可视化仪表盘全攻略
前端·人工智能·python·echarts·vue3·ai全栈·ai测试全栈
尽欢i1 天前
用 return“瘦身“if-else:让代码少嵌套、好维护
前端·javascript
程序员Agions1 天前
小程序"邪修"秘籍:那些官方文档不会告诉你的骚操作
前端·javascript
Charlo1 天前
手把手配置 Ralph -- 火爆 X 的全自动 AI 编程工具
前端·后端·github
我真的叫奥运1 天前
scss mixin svg 颜色控制 以及与 png 方案对比讨论
前端·svg
雲墨款哥1 天前
从一行好奇的代码说起:React的 useEffect 到底是不是生命周期?
前端·react.js·设计模式
weixin_584121431 天前
HTML+layui表单校验范围值,根据条件显示隐藏某输入框
前端·html·layui
加油乐1 天前
react基础概念合集
前端·react.js