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>
相关推荐
Anesthesia丶19 小时前
Vite + Svelte + shadcn-svelte 最小化 Demo+Vue3语法对比总结
vue·vite·svelte·shadcn-svelte
孟郎郎19 小时前
TimeoutError: The operation was aborted due to timeout at new DOMException
ai·前端框架·npm·vue·pnpm·deepseek
lpd_lt1 天前
AI生成Spring Boot + Vue 3 + MySQL + MyBatis-Plus的项目实战
java·spring boot·vue·ai编程
来杯@Java1 天前
图书管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·mybatis·课程设计
华大哥2 天前
前后端分离实现五级行政区划树形菜单及设备查询管理
sqlite·vue·springboot
码界筑梦坊2 天前
282-基于Python的豆瓣音乐可视化分析推荐系统
开发语言·python·信息可视化·数据分析·flask·vue
chushiyunen2 天前
滑块验证(滑动验证)
vue
Curvatureflight4 天前
前端国际化 i18n 落地实践:语言包、动态文案和格式化问题怎么处理?
前端·c++·vue
优雅格子衫4 天前
uniapp 拍照相册选取后超级好用的裁剪组件,增加水印完全自定义
开发语言·前端·javascript·uni-app·vue
路光.4 天前
uniapp中解决webview在app中调用,有过渡空白问题,增加过渡动效
uni-app·vue·app·uniapp