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>
相关推荐
Bl_a_ck5 天前
npm、nvm、nrm
前端·vue.js·npm·node.js·vue
小七de尾巴5 天前
利用pnpm patch给第三方库打补丁
vue·pnpm·patch·补丁
ILUUSION_S5 天前
Vue接口平台学习七——接口调试页面请求体
vue.js·vue
来自星星的坤5 天前
SpringBoot 与 Vue3 实现前后端互联全解析
后端·ajax·前端框架·vue·springboot
XDIGAS5 天前
Dockerfile项目实战-单阶段构建Vue2项目
docker·容器·centos·node.js·vue
风清云淡_A6 天前
【vue3】vue3+express实现图片/pdf等资源文件的下载
javascript·vue
没事别学JAVA6 天前
vue3环境搭建、nodejs22.x安装、yarn 1全局安装、npm切换yarn 1、yarn 1 切换npm
vue.js·node.js·vue
百锦再6 天前
Python实现浏览器模拟访问及页面解析的全面指南
开发语言·前端·javascript·python·vue·框架·react
不掰手腕7 天前
vue+leaflet 区域划分_反向遮罩层
vue
有什么东东7 天前
山东大学软件学院创新项目实训开发日志(12)之将对话记录保存到数据库中
java·数据库·vue·springboot