Vue3当中el-tree树形控件使用

tree悬停tooltip效果

javascript 复制代码
文本过长超出展示省略号 
如果文本超出悬停显示tooltip效果 反之不显示
这里直接控制固定宽度限制 试了监听宽度没效果
javascript 复制代码
<template>
  <el-tree
    show-checkbox
    :check-strictly="true"
    :data="data"
    node-key="id"
    :props="defaultProps"
  >
    <template #default="{ node }">
      <span>
        <el-tooltip :content="node.label" :disabled="!isTextOverflow(node.label)" placement="top">
          <span class="tree-node-label">{{ node.label }}</span>
        </el-tooltip>
      </span>
    </template>
  </el-tree>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const data = ref([
      { id: 1, name: 'Parent Node with very long text content that may overflow' },
      { 
        id: 2, 
        name: 'Parent',
        children: [
          { id: 21, name: 'Child Node with normal length' }
        ]
      }
    ]);

    const maxCharLength = ref(10);//定义最大字符 根据需求可调整

    const defaultProps = {
      label: 'name',
      children: 'children'
    };

    // 检测文本是否溢出
    const isTextOverflow = (label) => {
      return label.length > maxCharLength.value;
    };

    return {
      data,
      defaultProps,
      isTextOverflow
    };
  }
};
</script>

<style scoped>
.tree-node-label {
  display: inline-block;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
}
</style>
相关推荐
ok406lhq几秒前
用 MonkeyCode 8 小时搭建自动化内容站:AI Coding 平台实战复盘
ci/cd·vue·ai编程·自动化部署·monkeycode
brycegao3219 小时前
Tauri2+Vue3+Ollama 实战|依托 AI 协同开发全离线隐私记账桌面软件(开源)
人工智能·开源·vue·ai编程·tauri·ollama·桌面开发
向日的葵00614 小时前
快速了解vue中的路由如何实现(路由一)
前端·vue.js·vue·路由
暗冰ཏོ15 小时前
《uni-app 跨端开发完整指南:从基础入门到 H5、小程序、App 发布上线》
前端·小程序·uni-app·vue·html5
文阿花1 天前
大屏地图实现方案之-高德(二)
vue·地图·高德
森林的尽头是阳光1 天前
前端使用postman快速造数据
前端·javascript·vue·postman·造数·本地测试
文阿花2 天前
大屏实现方案之-高德
vue·地图·高德
Anesthesia丶4 天前
Vite + Svelte + shadcn-svelte 最小化 Demo+Vue3语法对比总结
vue·vite·svelte·shadcn-svelte
孟郎郎4 天前
TimeoutError: The operation was aborted due to timeout at new DOMException
ai·前端框架·npm·vue·pnpm·deepseek
lpd_lt4 天前
AI生成Spring Boot + Vue 3 + MySQL + MyBatis-Plus的项目实战
java·spring boot·vue·ai编程