vue 树组件 vxe-tree 使用节点连接线

0 阅读4分钟

树组件用缩进表达层级,层级较深时不够直观。vxe-tree 通过 show-line 开启节点连接线,用线条清晰展示父子关系;同时支持 icon-open / icon-close 自定义展开、收起图标。连接线与图标可同时使用,也可配合多选、transform 平铺数据等能力。

vxe-tree 如何开启节点连接线?

在 上添加 show-line 属性即可显示节点连接线。若数据为平铺数组,同时添加 transform,组件会按 id / parentId 自动构建树形结构。

*数据需形成正确的父子关系:transform 模式下,parentId 为 null 的节点视为根节点。

  • 每个节点必须有唯一 id,parentId 需指向真实存在的父节点 id,否则该节点不会挂载到树中,连接线也无从绘制。
  • 节点标题字段默认使用 title。

预期结果:节点左侧出现竖向与横向连接线,层级关系一目了然;展开、收起时连接线随之调整。

vxe-tree 如何自定义展开 / 收起图标?

通过 icon-open 与 icon-close 属性分别指定展开态与收起态的图标类名。示例中使用 vxe-icon-square-minus 与 vxe-icon-square-plus,呈现“方块减号 / 方块加号”的展开收起效果。

  • 图标类名需为项目中已引入的图标库类名,vxe 官方图标以 vxe-icon- 开头。
  • 节点存在子节点时才会显示展开 / 收起图标;叶子节点不显示。

有子节点的行前显示指定图标,点击切换展开状态,图标随状态在 icon-open 与 icon-close 之间切换。

代码

image

<template>
  <div>
    <vxe-tree :data="treeList" show-line transform></vxe-tree>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'

const treeList = ref([
  { title: '节点2', id: '2', parentId: null },
  { title: '节点3', id: '3', parentId: null },
  { title: '节点3-1', id: '31', parentId: '3' },
  { title: '节点3-2', id: '32', parentId: '3' },
  { title: '节点3-2-1', id: '321', parentId: '32' },
  { title: '节点3-2-2', id: '322', parentId: '32' },
  { title: '节点3-3', id: '33', parentId: '3' },
  { title: '节点3-3-1', id: '331', parentId: '33' },
  { title: '节点3-3-2', id: '332', parentId: '33' },
  { title: '节点3-3-3', id: '333', parentId: '33' },
  { title: '节点3-4', id: '34', parentId: '3' },
  { title: '节点4', id: '4', parentId: null },
  { title: '节点4-1', id: '41', parentId: '4' },
  { title: '节点4-1-1', id: '411', parentId: '42' },
  { title: '节点4-1-2', id: '412', parentId: '42' },
  { title: '节点4-2', id: '42', parentId: '4' },
  { title: '节点4-3', id: '43', parentId: '4' },
  { title: '节点4-3-1', id: '431', parentId: '43' },
  { title: '节点4-3-2', id: '432', parentId: '43' },
  { title: '节点5', id: '5', parentId: null }
])
</script>

多选

image

<template>
  <div>
    <vxe-tree :data="treeList" icon-open="vxe-icon-square-minus" icon-close="vxe-icon-square-plus" transform show-checkbox show-line></vxe-tree>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const treeList = ref([
  { title: '节点2', id: '2', parentId: null },
  { title: '节点3', id: '3', parentId: null },
  { title: '节点3-1', id: '31', parentId: '3' },
  { title: '节点3-2', id: '32', parentId: '3' },
  { title: '节点3-2-1', id: '321', parentId: '32' },
  { title: '节点3-2-2', id: '322', parentId: '32' },
  { title: '节点3-3', id: '33', parentId: '3' },
  { title: '节点3-3-1', id: '331', parentId: '33' },
  { title: '节点3-3-2', id: '332', parentId: '33' },
  { title: '节点3-3-3', id: '333', parentId: '33' },
  { title: '节点3-4', id: '34', parentId: '3' },
  { title: '节点4', id: '4', parentId: null },
  { title: '节点4-1', id: '41', parentId: '4' },
  { title: '节点4-1-1', id: '411', parentId: '42' },
  { title: '节点4-1-2', id: '412', parentId: '42' },
  { title: '节点4-2', id: '42', parentId: '4' },
  { title: '节点4-3', id: '43', parentId: '4' },
  { title: '节点4-3-1', id: '431', parentId: '43' },
  { title: '节点4-3-2', id: '432', parentId: '43' },
  { title: '节点5', id: '5', parentId: null }
])
</script>

image

参数

属性类型默认值说明
dataArray[]树数据,每个节点需含唯一 id、parentId、title
show-lineBooleanfalse是否显示节点连接线
transformBooleanfalse将平铺数组转换为树形结构,依赖 id / parentId
show-checkboxBooleanfalse是否显示复选框,支持多选
icon-openString组件默认展开状态图标类名
icon-closeString组件默认收起状态图标类名
节点 idString / Number—节点唯一标识,必须唯一
节点 parentIdString / Number / null—父节点 id,null 表示根节点
节点 titleString—节点显示文本

连接线与自定义图标的配合

  • 连接线位置:连接线绘制在缩进区域内,位于展开图标左侧或与图标对齐,具体视觉以实际版本为准。
  • 图标与线条同时存在:自定义图标替换默认展开 / 收起图标后,连接线样式不受影响,二者独立渲染。
  • 复选框列:开启 show-checkbox 后,复选框位于最左侧,展开图标、连接线依次排列,需确保容器宽度足够,避免拥挤。

vxeui.com