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>
相关推荐
你脸上有BUG9 小时前
TreeSelect 组件 showCheckedStrategy 属性不生效问题
前端·vue
૮・ﻌ・18 小时前
小兔鲜电商项目(一):项目准备、Layout模块、Home模块
前端·javascript·vue
无心使然云中漫步18 小时前
vant实现自定义日期时间选择器(年月日时分秒)
前端·vue
卡布叻_星星1 天前
创建基于 Vue3 且将 request.js(请求封装)与页面 .vue 解耦的项目
vue
LYFlied2 天前
Vue3虚拟DOM更新机制源码深度解析
前端·算法·面试·vue·源码解读
@AfeiyuO2 天前
Vue3 玫瑰图
vue·echarts
running up4 天前
Pinia 完整使用指南
vue
安_4 天前
<style scoped>跟<style>有什么区别
前端·vue
辛-夷4 天前
TS封装axios
前端·vue.js·typescript·vue·axios
@AfeiyuO4 天前
Vue3 矩形树图
vue·echarts