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>
相关推荐
华玥作者18 小时前
从“碎片化”到“资产化”:Vue3 + UniApp 组件库的进化论
ui·uni-app·vue·组件库
薛定谔的猫喵喵19 小时前
从零到一:Spring Boot + Vue 项目前后端运行完整指南
vue.js·spring boot·后端·pycharm·vue
No8g攻城狮1 天前
【AI工具】wsl2 + ubuntu22.04安装部署sub2api详细教程
人工智能·ai·go·vue
审判长烧鸡2 天前
【AI问答/前端】前端满天过海局(一)
前端·vue·浏览器
审判长烧鸡2 天前
【AI问答/前端】前端瞒天过海局(三)
前端·vue·html5·js
弹简特3 天前
【Vue3速成】04-vue3官方库-路由机制
前端·vue·路由
shadow_glory4 天前
大屏邪修自适应
vue
是梦终空4 天前
计算机源码274—基于深度学习的中医舌象智能识别与健康管理系统(源代码+数据库+12000字论文)
人工智能·python·深度学习·opencv·django·vue·springboot
涵涵(互关)4 天前
Naive-ui树型选择器只显示根节点
前端·ui·vue