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>
相关推荐
铅笔侠_小龙虾2 小时前
Emmet 常用用法指南
前端·vue
怪我冷i6 小时前
Agent运行模式——ReAct和Plan-and-Execute
vue·agent·ai编程·ai写作
铅笔侠_小龙虾14 小时前
Ubuntu 搭建前端环境&Vue实战
linux·前端·ubuntu·vue
冥界摄政王15 小时前
Cesium学习第一章 安装下载 基于vue3引入Cesium项目开发
vue·vue3·html5·webgl·cesium
IT教程资源D17 小时前
[N_093]基于springboot,vue的宠物商城
mysql·vue·前后端分离·宠物商城·springboot宠物商城
IT教程资源C17 小时前
(N_093)基于springboot,vue的宠物商城
mysql·vue·前后端分离·宠物商城·springboot宠物商城
Irene19912 天前
实用篇:vsCode 中连接 WSL 并快速开始一个 Vue3 新项目
git·vscode·vue·wsl
丸子哥哥2 天前
vue + uni-app:利用原生uni.chooseImage封装拍照功能的组件
微信小程序·uni-app·vue
天天向上vir2 天前
防抖与节流
前端·typescript·vue
@AfeiyuO2 天前
Vue3 饼图定制图
vue·echarts