Skip to content

Tree 树形控件

Tree 用于展示具有父子关系的层级数据。VISTA B 直接使用 Element Plus 的 el-tree,适合组织架构、权限范围、分类目录和资源树等场景。

基础用法

通过 data 传入节点数据,使用 props 指定子节点和标签字段。

业务中心
项目管理
项目看板
任务分配
报价配置
价目表
折扣规则
系统设置
角色权限
操作日志
vue
<template>
  <el-tree :data="data" :props="props" default-expand-all />
</template>

<script setup lang="ts">
const props = {
  children: 'children',
  label: 'label',
}

const data = [
  {
    label: '业务中心',
    children: [{ label: '项目管理' }, { label: '报价配置' }],
  },
]
</script>

可选择

设置 show-checkbox 后节点前会显示复选框。需要配合 node-key 使用默认勾选、获取勾选节点等能力。

业务中心
报价配置
vue
<template>
  <el-tree
    :data="data"
    node-key="id"
    show-checkbox
    :default-expanded-keys="[1]"
    :default-checked-keys="[3]"
  />
</template>

禁用节点

节点数据中设置 disabled 后,该节点不可被选中。

组织管理
总部
归档部门
权限范围
管理员
外部协作方
vue
<template>
  <el-tree :data="data" node-key="id" show-checkbox default-expand-all />
</template>

<script setup lang="ts">
const data = [
  {
    id: 1,
    label: '组织管理',
    children: [
      { id: 2, label: '总部' },
      { id: 3, label: '归档部门', disabled: true },
    ],
  },
]
</script>

默认展开和当前节点

通过 default-expanded-keys 控制默认展开节点,通过 current-node-keyhighlight-current 标记当前节点。

业务中心
项目管理
vue
<el-tree
  :data="data"
  node-key="id"
  highlight-current
  :default-expanded-keys="[1, 2]"
  :current-node-key="3"
/>

节点过滤

调用 Tree 实例的 filter 方法并提供 filter-node-method,可以按关键字过滤节点。

业务中心
项目管理
项目看板
任务分配
报价配置
价目表
折扣规则
系统设置
角色权限
操作日志
vue
<template>
  <el-input v-model="filterText" placeholder="输入关键字过滤" clearable />
  <el-tree
    ref="treeRef"
    :data="data"
    :filter-node-method="filterNode"
    default-expand-all
  />
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'

const treeRef = ref()
const filterText = ref('')

watch(filterText, (value) => {
  treeRef.value?.filter(value)
})

const filterNode = (value: string, data: { label: string }) => {
  if (!value) return true
  return data.label.includes(value)
}
</script>

手风琴模式

设置 accordion 后,同一层级每次只展开一个节点。

vue
<el-tree :data="data" accordion />

懒加载

设置 lazyload 后,节点会在展开时动态加载子节点。props.isLeaf 用于声明叶子节点。

vue
<template>
  <el-tree :props="props" lazy :load="loadNode" />
</template>

<script setup lang="ts">
const props = {
  label: 'label',
  children: 'children',
  isLeaf: 'isLeaf',
}

const loadNode = (node, resolve) => {
  if (node.level === 0) {
    resolve([{ label: '华东大区' }, { label: '华南大区' }])
    return
  }

  resolve([{ label: '门店 A', isLeaf: true }])
}
</script>

自定义节点内容

默认插槽接收 nodedata,可以在节点内展示图标、标签、数量或操作按钮。

业务中心目录
项目管理目录
项目看板页面
任务分配页面
报价配置目录
价目表页面
折扣规则页面
系统设置目录
角色权限页面
操作日志页面
vue
<el-tree :data="data" default-expand-all>
  <template #default="{ node, data }">
    <span class="custom-tree-node">
      <span>{{ node.label }}</span>
      <el-tag size="small">{{ data.type }}</el-tag>
    </span>
  </template>
</el-tree>

拖拽节点

设置 draggable 后可拖拽节点。通过 allow-dragallow-drop 可以限制可拖动节点与可放置位置。

门户导航
首页
项目中心
管理后台
用户管理
审计日志
vue
<template>
  <el-tree
    :data="data"
    node-key="id"
    draggable
    :allow-drag="allowDrag"
    :allow-drop="allowDrop"
    @node-drop="handleDrop"
  />
</template>

<script setup lang="ts">
const allowDrag = (node) => !node.data.disabled
const allowDrop = (_draggingNode, _dropNode, type) => type !== 'inner'
const handleDrop = (draggingNode, dropNode, dropType) => {
  console.log(draggingNode.data, dropNode.data, dropType)
}
</script>

空状态

没有节点数据时会展示 empty-text,也可以通过 empty 插槽自定义空内容。

暂无组织数据

暂无组织数据

vue
<el-tree :data="[]" empty-text="暂无组织数据">
  <template #empty>
    <HomeVistaEmpty title="暂无组织数据" :image-size="120" />
  </template>
</el-tree>

API

Tree 属性

属性说明类型默认值
data展示数据array[]
empty-text内容为空时显示的文本string暂无数据
node-key每个树节点的唯一标识字段,使用勾选、当前节点等能力时必填string-
props配置选项,指定节点字段映射object-
render-after-expand是否在第一次展开某个节点后才渲染其子节点booleantrue
load懒加载子节点的方法function-
render-content自定义树节点内容的渲染函数function-
highlight-current是否高亮当前选中节点booleanfalse
default-expand-all是否默认展开所有节点booleanfalse
expand-on-click-node是否在点击节点时展开或收起节点booleantrue
check-on-click-node是否在点击节点时选中复选框booleanfalse
check-on-click-leaf是否在点击叶子节点时选中复选框booleantrue
auto-expand-parent展开子节点时是否自动展开父节点booleantrue
default-expanded-keys默认展开节点的 key 数组array[]
show-checkbox节点是否可被选择booleanfalse
check-strictly是否严格遵循父子节点不互相关联booleanfalse
check-descendants设置 check-strictly 时,是否仍检查后代节点状态booleanfalse
default-checked-keys默认勾选节点的 key 数组array[]
current-node-key当前选中节点的 keystring | number-
filter-node-method过滤节点的方法function-
accordion是否每次只打开同级树节点中的一个booleanfalse
indent相邻层级节点间的水平缩进,单位 pxnumber18
icon自定义树节点展开图标string | Component-
lazy是否懒加载子节点booleanfalse
draggable是否开启节点拖拽booleanfalse
allow-drag判断节点是否允许拖拽的方法function-
allow-drop拖拽时判断目标节点是否可被放置的方法function-

Tree 事件

事件名说明类型
node-click节点被点击时触发Function
node-contextmenu节点被右键点击时触发Function
check-change节点勾选状态变化时触发Function
check点击节点复选框后触发Function
current-change当前选中节点变化时触发Function
node-expand节点被展开时触发Function
node-collapse节点被收起时触发Function
node-drag-start节点开始拖拽时触发Function
node-drag-enter拖拽节点进入目标节点时触发Function
node-drag-leave拖拽节点离开目标节点时触发Function
node-drag-over拖拽节点悬停在目标节点时触发Function
node-drag-end节点拖拽结束时触发Function
node-drop节点拖拽放置成功时触发Function

Tree 插槽

插槽说明
default自定义树节点内容,作用域参数为 { node, data }
empty自定义空状态内容

Tree 暴露

名称说明类型
filter对树节点进行过滤Function
getNodeKey获取节点的 keyFunction
getNodePath获取节点路径Function
getCheckedNodes获取当前被勾选的节点数组Function
getCheckedKeys获取当前被勾选节点的 key 数组Function
getCurrentNode获取当前选中节点Function
getCurrentKey获取当前选中节点的 keyFunction
setCheckedNodes设置当前勾选节点数组Function
setCheckedKeys通过 key 数组设置当前勾选节点Function
setChecked设置单个节点的勾选状态Function
getHalfCheckedNodes获取当前半选中的节点数组Function
getHalfCheckedKeys获取当前半选中节点的 key 数组Function
setCurrentNode设置当前选中节点Function
setCurrentKey通过 key 设置当前选中节点Function
getNode根据 data 或 key 获取节点Function
remove删除节点Function
append为指定节点追加子节点Function
insertBefore在指定节点前插入节点Function
insertAfter在指定节点后插入节点Function
updateKeyChildren通过 key 更新某个节点的子节点Function

Props 配置

属性说明类型默认值
label指定节点标签字段string | functionlabel
children指定子节点字段stringchildren
disabled指定禁用节点字段string | functiondisabled
isLeaf指定叶子节点字段,在懒加载模式下生效string | functionisLeaf
class指定节点自定义 class 字段string | function-

HOMEVISTA 设计规范