OrganizationChart 组织架构图
按树形数据画出部门、人员的上下级关系:上级在上,下级在下面横向排开,中间用线连起来。节点可以自定义内容、折叠、选中,也可以改成从左到右横着排。
引入
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
基础用法
data 传根节点,每个节点要有唯一的 id,label 是卡片上的文字,children 是下级。卡片默认是白底、细边框、顶上一条主题色,连线用 CSS 画。
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' },
{ id: 'qa', label: '测试组' }
]
},
{
id: 'mk',
label: '市场部',
children: [
{ id: 'east', label: '华东大区' },
{ id: 'south', label: '华南大区' }
]
},
{ id: 'hr', label: '人事部' }
]
}
</script>
<template>
<HuiOrganizationChart :data="data" aria-label="星河科技组织架构" />
</template>
多个根节点
data 也可以是数组,几个根节点并排显示,各自是一棵独立的树。
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData[] = [
{
id: 'bj',
label: '北京分公司',
children: [
{ id: 'bj-sales', label: '销售部' },
{ id: 'bj-service', label: '客服部' }
]
},
{
id: 'sh',
label: '上海分公司',
children: [{ id: 'sh-sales', label: '销售部' }]
}
]
</script>
<template>
<HuiOrganizationChart :data="data" />
</template>
自定义字段名
数据里的字段名不是 id、label、children、disabled 时,用 props 指定:key 是唯一标识,label 是文字,children 是下级,disabled 是禁用。
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
deptCode: 'HQ',
deptName: '集团总部',
subDepts: [
{ deptCode: 'FIN', deptName: '财务中心' },
{
deptCode: 'OPS',
deptName: '运营中心',
subDepts: [
{ deptCode: 'OPS-1', deptName: '客服部' },
{ deptCode: 'OPS-2', deptName: '物流部' }
]
}
]
}
const fieldNames = { key: 'deptCode', label: 'deptName', children: 'subDepts' }
</script>
<template>
<HuiOrganizationChart :data="data" :props="fieldNames" />
</template>
自定义节点
默认插槽自定义卡片里的内容,参数有节点数据 node、层级 level(根节点是 0)、expanded、selected、disabled。卡片的边框、圆角、主题色和选中高亮还在,插槽只管里面放什么。下面每个节点放了头像、姓名和职位,有下属的再显示人数:
<script setup lang="ts">
import { HuiAvatar, HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 1,
name: '王建国',
title: '总经理',
children: [
{
id: 2,
name: '李晓明',
title: '技术总监',
children: [
{ id: 4, name: '陈思', title: '前端负责人' },
{ id: 5, name: '赵磊', title: '后端负责人' }
]
},
{
id: 3,
name: '刘芳',
title: '市场总监',
children: [{ id: 6, name: '孙悦', title: '品牌经理' }]
}
]
}
const firstChar = (node: OrganizationChartNodeData) => String(node.name).slice(0, 1)
const count = (node: OrganizationChartNodeData) =>
Array.isArray(node.children) ? node.children.length : 0
</script>
<template>
<HuiOrganizationChart :data="data">
<template #default="{ node, level }">
<div class="person">
<HuiAvatar :label="firstChar(node)" :size="level === 0 ? 'default' : 'small'" />
<div class="person__text">
<div class="person__name">{{ node.name }}</div>
<div class="person__title">
{{ node.title }}<template v-if="count(node)"> · {{ count(node) }} 人</template>
</div>
</div>
</div>
</template>
</HuiOrganizationChart>
</template>
<style scoped>
.person {
display: flex;
gap: var(--hui-spacing-2);
align-items: center;
text-align: left;
}
.person__name {
font-weight: var(--hui-font-weight-semibold);
}
.person__title {
font-size: var(--hui-font-size-sm);
color: var(--hui-text-color-secondary);
white-space: nowrap;
}
</style>
节点类名
node-class 给卡片加类名:传字符串时每个卡片都加,传函数时按节点返回,参数是节点数据和层级,返回值和模板里 class 的写法一样(字符串、数组、对象)。类名加在卡片 .hui-organization-chart__card 上。组件的样式带 scoped,自己的类要写在全局样式里,或者在 scoped 样式里用 :deep(),选择器前面再带一个类,比卡片自己的样式优先。
下面公司用主色底,部门顶上一条绿色,小组顶上一条橙色:
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
type: 'company',
children: [
{
id: 'rd',
label: '研发中心',
type: 'dept',
children: [
{ id: 'fe', label: '前端组', type: 'team' },
{ id: 'be', label: '后端组', type: 'team' }
]
},
{
id: 'mk',
label: '市场部',
type: 'dept',
children: [{ id: 'east', label: '华东大区', type: 'team' }]
}
]
}
const nodeClass = (node: OrganizationChartNodeData) => `org-card--${node.type}`
</script>
<template>
<HuiOrganizationChart class="org-chart" :data="data" :node-class="nodeClass" />
</template>
<style scoped>
.org-chart :deep(.org-card--company) {
color: var(--hui-color-white);
background-color: var(--hui-color-primary);
}
.org-chart :deep(.org-card--dept) {
border-top-color: var(--hui-color-success);
}
.org-chart :deep(.org-card--team) {
border-top-color: var(--hui-color-warning);
}
</style>
可折叠
加上 collapsible 后,有下级的节点卡片下边多一个小圆钮,点它展开、收起下级(显示减号时是展开着的)。收起的那一支不渲染。不加 collapsible 时始终全部展开,没有按钮。
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' }
]
},
{
id: 'mk',
label: '市场部',
children: [
{ id: 'east', label: '华东大区' },
{ id: 'south', label: '华南大区' }
]
},
{ id: 'hr', label: '人事部' }
]
}
</script>
<template>
<HuiOrganizationChart :data="data" collapsible />
</template>
默认展开层级
default-expand-level 是默认展开的层数,根节点算第 1 层:0 时全部收起,只显示根节点;1 时只展开根节点,能看到第 2 层;2 时展开到第 2 层,能看到第 3 层;不传时全部展开。它只决定一开始的状态,下面切换时用 key 让图重新创建。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{
id: 'fe',
label: '前端组',
children: [
{ id: 'web', label: 'Web 小组' },
{ id: 'app', label: 'App 小组' }
]
},
{ id: 'be', label: '后端组' }
]
},
{
id: 'mk',
label: '市场部',
children: [{ id: 'east', label: '华东大区' }]
}
]
}
const level = ref<string | number>(1)
const options = [
{ label: '0', value: 0 },
{ label: '1', value: 1 },
{ label: '2', value: 2 },
{ label: '不传(全部展开)', value: 'all' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="level" :options="options" />
<HuiOrganizationChart
:key="level"
:data="data"
collapsible
:default-expand-level="level === 'all' ? undefined : Number(level)"
/>
</HuiSpace>
</template>
默认展开指定节点
default-expanded-keys 指定一开始展开哪些节点,它们的上级也会跟着展开,其余收起。和 default-expand-level 同时传时,两者指定的节点都展开。下面只展开“前端组”,所以能看到它的两个小组,“市场部”是收起的:
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{
id: 'fe',
label: '前端组',
children: [
{ id: 'web', label: 'Web 小组' },
{ id: 'app', label: 'App 小组' }
]
},
{ id: 'be', label: '后端组' }
]
},
{
id: 'mk',
label: '市场部',
children: [{ id: 'east', label: '华东大区' }]
}
]
}
</script>
<template>
<HuiOrganizationChart :data="data" collapsible :default-expanded-keys="['fe']" />
</template>
受控展开和方法
v-model:expanded-keys 双向绑定展开的节点,传了以后由它决定展开状态,default-expand-level、default-expanded-keys 不再起作用。也可以通过 ref 调用方法:expand(key) 展开节点和它的上级,collapse(key) 收起,expandAll()、collapseAll() 全部展开、收起,getExpandedKeys() 读取展开的节点。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type {
OrganizationChartExpose,
OrganizationChartKey,
OrganizationChartNodeData
} from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' }
]
},
{
id: 'mk',
label: '市场部',
children: [
{ id: 'east', label: '华东大区' },
{ id: 'south', label: '华南大区' }
]
}
]
}
const chartRef = ref<OrganizationChartExpose>()
const expandedKeys = ref<OrganizationChartKey[]>(['co'])
const result = ref('')
const read = () => {
result.value = `getExpandedKeys():${JSON.stringify(chartRef.value?.getExpandedKeys())}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="chartRef?.expandAll()">全部展开</HuiButton>
<HuiButton @click="chartRef?.collapseAll()">全部收起</HuiButton>
<HuiButton @click="chartRef?.expand('east')">展开到华东大区</HuiButton>
<HuiButton @click="chartRef?.collapse('rd')">收起研发中心</HuiButton>
<HuiButton @click="expandedKeys = ['co', 'mk']">只展开市场部</HuiButton>
<HuiButton @click="read">读取展开的节点</HuiButton>
</HuiSpace>
<HuiOrganizationChart
ref="chartRef"
v-model:expanded-keys="expandedKeys"
:data="data"
collapsible
/>
<p>v-model:{{ expandedKeys }}</p>
<p>{{ result || '点“读取展开的节点”试试' }}</p>
</HuiSpace>
</template>
单选
selection-mode="single" 后点击卡片选中它,选中的卡片是主色边框、浅主色底;点别的节点换成选中别的,再点一次已选中的节点取消选中。用 v-model:selection 拿到选中节点的 key,没选中时是 null。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartSelection } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' }
]
},
{ id: 'mk', label: '市场部' },
{ id: 'hr', label: '人事部' }
]
}
const selection = ref<OrganizationChartSelection>('rd')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiOrganizationChart v-model:selection="selection" :data="data" selection-mode="single" />
<p>选中的 key:{{ selection ?? '无' }}</p>
</HuiSpace>
</template>
多选
selection-mode="multiple" 后点击卡片在选中和不选中之间切换,可以选多个,v-model:selection 是 key 数组。节点数据里 disabled 为 true 的不能选中,点击也不触发 nodeClick。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartSelection } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' },
{ id: 'qa', label: '测试组' }
]
},
{ id: 'mk', label: '市场部' },
{ id: 'legal', label: '法务部(停用)', disabled: true }
]
}
const selection = ref<OrganizationChartSelection>(['fe', 'mk'])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiOrganizationChart v-model:selection="selection" :data="data" selection-mode="multiple" />
<p>选中的 key:{{ selection }}</p>
</HuiSpace>
</template>
事件
点击、展开、收起、选中都会触发事件,参数里有节点数据 node 和它的 key。下面把最近的几次事件列出来:
nodeClick:点卡片、或者聚焦节点后按回车、空格时触发,参数还有层级level。点展开按钮、点禁用的节点不触发。nodeExpand、nodeCollapse:点按钮、按键展开或收起时触发。调用方法展开、收起时不触发。selectionChange:选中的节点变了时触发,selection是操作后的值,nodes是选中的节点数据。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type {
OrganizationChartNodeClickPayload,
OrganizationChartNodeData,
OrganizationChartNodeTogglePayload,
OrganizationChartSelectionChangePayload
} from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' }
]
},
{
id: 'mk',
label: '市场部',
children: [{ id: 'east', label: '华东大区' }]
}
]
}
const events = ref<string[]>([])
const record = (text: string) => {
events.value = [text, ...events.value].slice(0, 6)
}
const onClick = ({ node, level }: OrganizationChartNodeClickPayload) =>
record(`nodeClick:${node.label}(第 ${level + 1} 层)`)
const onExpand = ({ node }: OrganizationChartNodeTogglePayload) =>
record(`nodeExpand:${node.label}`)
const onCollapse = ({ node }: OrganizationChartNodeTogglePayload) =>
record(`nodeCollapse:${node.label}`)
const onSelectionChange = ({ nodes }: OrganizationChartSelectionChangePayload) =>
record(`selectionChange:选中 ${nodes.map((node) => node.label).join('、') || '无'}`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiOrganizationChart
:data="data"
collapsible
selection-mode="multiple"
@node-click="onClick"
@node-expand="onExpand"
@node-collapse="onCollapse"
@selection-change="onSelectionChange"
/>
<div>
<p v-if="events.length === 0">点卡片、点展开按钮试试</p>
<p v-for="(text, index) in events" :key="index" style="margin: 0 0 4px">{{ text }}</p>
</div>
</HuiSpace>
</template>
横向
direction 是排列方向:vertical(默认)上级在上、下级在下面横向排开;horizontal 上级在左、下级在右边竖着排,适合层级多、每层节点少的结构。横向时展开按钮在卡片右边,键盘的方向也跟着转过来(见“键盘操作”)。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiOrganizationChart, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartDirection, OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' },
{ id: 'qa', label: '测试组' }
]
},
{
id: 'mk',
label: '市场部',
children: [
{ id: 'east', label: '华东大区' },
{ id: 'south', label: '华南大区' }
]
},
{ id: 'hr', label: '人事部' }
]
}
const direction = ref<OrganizationChartDirection>('horizontal')
const options = [
{ label: '竖向(vertical)', value: 'vertical' },
{ label: '横向(horizontal)', value: 'horizontal' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="direction" :options="options" />
<HuiOrganizationChart :data="data" :direction="direction" collapsible />
</HuiSpace>
</template>
宽内容滚动
内容比外面宽时,组件里出现横向滚动条;给组件设了高度(或 max-height)时,内容更高也在组件里上下滚动。内容比外面窄时居中显示(横向时靠左)。
focus(key) 方法让节点获得焦点,并把它的卡片滚到能看见的位置;节点被收起看不到时先展开它的上级。下面一个总部带 12 个事业部,点按钮定位到某个事业部并选中它:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type {
OrganizationChartExpose,
OrganizationChartKey,
OrganizationChartNodeData,
OrganizationChartSelection
} from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'root',
label: '集团总部',
children: Array.from({ length: 12 }, (_, i) => ({
id: `d${i + 1}`,
label: `第 ${i + 1} 事业部`,
children: [
{ id: `d${i + 1}-product`, label: '产品组' },
{ id: `d${i + 1}-ops`, label: '运营组' }
]
}))
}
const chartRef = ref<OrganizationChartExpose>()
const selection = ref<OrganizationChartSelection>(null)
const locate = (key: OrganizationChartKey) => {
selection.value = key
chartRef.value?.focus(key)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @click="locate('d1')">定位到第 1 事业部</HuiButton>
<HuiButton @click="locate('d7')">定位到第 7 事业部</HuiButton>
<HuiButton @click="locate('d12-ops')">定位到第 12 事业部运营组</HuiButton>
</HuiSpace>
<HuiOrganizationChart
ref="chartRef"
v-model:selection="selection"
:data="data"
selection-mode="single"
collapsible
:default-expand-level="1"
style="max-height: 280px; border: 1px solid var(--hui-border-color-light)"
/>
</HuiSpace>
</template>
间距和连线颜色
在组件自身或任意祖先上设置这几个CSS变量,调整上下级之间的距离、同一层相邻两支之间的距离和连线颜色。组件不在根上重定义公开值,递归节点和展开按钮均继承同一配置;未设置时使用下表默认值。嵌套的组织图也会继承祖先配置,需要不同取值时在内层重新设置。下面把变量写在祖先容器:
| 变量 | 默认值 | 说明 |
|---|---|---|
--hui-organization-chart-level-gap | var(--hui-spacing-10) | 上下级卡片之间的距离(横向时是左右),40px |
--hui-organization-chart-node-gap | var(--hui-spacing-4) | 同一层相邻两支之间的距离(横向时是上下),16px |
--hui-organization-chart-line-color | var(--hui-border-color) | 连线和展开按钮边框的颜色 |
<script setup lang="ts">
import { HuiOrganizationChart } from '@nscode/handy-ui'
import type { OrganizationChartNodeData } from '@nscode/handy-ui'
const data: OrganizationChartNodeData = {
id: 'co',
label: '星河科技',
children: [
{
id: 'rd',
label: '研发中心',
children: [
{ id: 'fe', label: '前端组' },
{ id: 'be', label: '后端组' }
]
},
{ id: 'mk', label: '市场部' }
]
}
</script>
<template>
<div
style="--hui-organization-chart-level-gap: 64px; --hui-organization-chart-node-gap: 40px; --hui-organization-chart-line-color: var(--hui-color-primary)"
>
<HuiOrganizationChart :data="data" />
</div>
</template>
键盘操作
整张图只占一个 Tab 停留点:按 Tab 进来时,焦点落在上次聚焦过的节点上;第一次进来、或那个节点已经看不到时,落在第一个能看到的选中节点上,再没有就是第一个根节点。聚焦的卡片有主色焦点框,并会滚到能看见的位置。在图里用方向键移动焦点:
| 按键(竖向) | 按键(横向) | 作用 |
|---|---|---|
| ↑ | ← | 焦点回到上级 |
| ↓ | → | 节点收起着时先展开它;已经展开时焦点移到第一个下级 |
| ←、→ | ↑、↓ | 焦点移到同一层的前一个、后一个能看到的节点,可以跨到别的上级下面 |
| Home、End | Home、End | 焦点移到第一个根节点、最后一个能看到的节点 |
| Enter、空格 | Enter、空格 | 相当于点击卡片:开了 selection-mode 时选中或取消,触发 nodeClick |
| +、- | +、- | 展开、收起(开了 collapsible 时) |
带 Ctrl、Alt、Meta 的组合键不处理,留给浏览器。焦点在自定义内容里的按钮、输入框上时,按键也留给它们自己。
选择自持与受控
selection独立按属性是否传入判断。未传或仅监听时保存内部选择;传入(含undefined或null)即受控,空值没有选中节点。父接受、改写或拒绝后,高亮、ARIA及插槽selected都读取实际值;selectionChange继续报告用户请求的选择及节点。展开配置不受这一选择合同影响。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartSelection } from '@nscode/handy-ui'
const selectionData: OrganizationChartNodeData[] = [
{ id: 'a', label: '甲' },
{ id: 'b', label: '乙' },
{ id: 'c', label: '丙' }
]
const selectionSingle = ref<OrganizationChartSelection>('a')
const selectionMultiple = ref<OrganizationChartSelection>(['a'])
const selectionRedraw = ref(0)
const selectionSingleLast = ref('暂无')
const selectionMultipleLast = ref('暂无')
const recordSelectionSingle = (value: OrganizationChartSelection) => {
selectionSingleLast.value = JSON.stringify(value)
}
const recordSelectionMultiple = (value: OrganizationChartSelection) => {
selectionMultipleLast.value = JSON.stringify(value)
}
</script>
<template>
<HuiSpace class="organization-selection-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="selection-redraw" @click="selectionRedraw++"
>父级重绘 {{ selectionRedraw }}</HuiButton
>
<span>点击乙后重绘父级,比较内部保存和父组件实际选择。</span>
</HuiSpace>
<HuiSpace wrap align="start">
<HuiSpace class="selection-single" direction="vertical" style="width: 370px; max-width: 100%">
<strong>单选</strong>
<HuiSpace class="selection-local" direction="vertical">
<span>未传选择</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
/>
</HuiSpace>
<HuiSpace class="selection-listener" direction="vertical">
<span>仅监听更新</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
@update:selection="recordSelectionSingle"
/>
</HuiSpace>
<HuiSpace class="selection-empty" direction="vertical">
<span>显式 undefined</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
:selection="undefined"
/>
</HuiSpace>
<HuiSpace class="selection-fixed" direction="vertical">
<span>固定甲,父组件拒绝更新</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
selection="a"
/>
</HuiSpace>
<HuiSpace class="selection-bound" direction="vertical">
<span>双向绑定</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="single"
v-model:selection="selectionSingle"
/>
</HuiSpace>
<span class="selection-status"
>监听请求:{{ selectionSingleLast }};绑定值:{{ selectionSingle }}</span
>
</HuiSpace>
<HuiSpace
class="selection-multiple"
direction="vertical"
style="width: 370px; max-width: 100%"
>
<strong>多选</strong>
<HuiSpace class="selection-local" direction="vertical">
<span>未传选择</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
/>
</HuiSpace>
<HuiSpace class="selection-listener" direction="vertical">
<span>仅监听更新</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
@update:selection="recordSelectionMultiple"
/>
</HuiSpace>
<HuiSpace class="selection-empty" direction="vertical">
<span>显式 undefined</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
:selection="undefined"
/>
</HuiSpace>
<HuiSpace class="selection-fixed" direction="vertical">
<span>固定甲,父组件拒绝更新</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
:selection="['a']"
/>
</HuiSpace>
<HuiSpace class="selection-bound" direction="vertical">
<span>双向绑定</span>
<HuiOrganizationChart
:data="selectionData"
:data-parent="selectionRedraw"
selection-mode="multiple"
v-model:selection="selectionMultiple"
/>
</HuiSpace>
<span class="selection-status"
>监听请求:{{ selectionMultipleLast }};绑定值:{{ selectionMultiple }}</span
>
</HuiSpace>
</HuiSpace>
</HuiSpace>
</template>
展开自持与受控
expandedKeys与selection分别判断控制方式。未传或仅监听时保存内部展开;显式传入(含undefined为空)由父组件决定。默认配置在创建时读取并复制,数据后来到达或节点第一次出现下级时,仍按该创建配置初始化;已经操作过的节点保留用户展开状态,初始化不额外通知更新。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiOrganizationChart, HuiSpace } from '@nscode/handy-ui'
import type { OrganizationChartNodeData, OrganizationChartKey } from '@nscode/handy-ui'
const expandedData: OrganizationChartNodeData = {
id: 'r',
label: '根',
children: [
{ id: 'a', label: '甲', children: [{ id: 'a1', label: '甲一' }] },
{ id: 'b', label: '乙' }
]
}
const expandedBound = ref<OrganizationChartKey[]>([])
const expandedRedraw = ref(0)
const expandedLast = ref('暂无')
const recordExpanded = (keys: OrganizationChartKey[]) => {
expandedLast.value = JSON.stringify(keys)
}
</script>
<template>
<HuiSpace class="organization-expanded-q1-demo" direction="vertical" fill>
<HuiSpace wrap>
<HuiButton class="expanded-redraw" @click="expandedRedraw++"
>父级重绘 {{ expandedRedraw }}</HuiButton
>
<span>聚焦根节点按加号或点展开按钮,再重绘父级比较实际展开。</span>
</HuiSpace>
<HuiSpace class="expanded-local" direction="vertical">
<span>未传展开属性</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
/>
</HuiSpace>
<HuiSpace class="expanded-listener" direction="vertical">
<span>仅监听更新</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
@update:expanded-keys="recordExpanded"
/>
</HuiSpace>
<HuiSpace class="expanded-empty" direction="vertical">
<span>显式 undefined</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
:expanded-keys="undefined"
/>
</HuiSpace>
<HuiSpace class="expanded-fixed" direction="vertical">
<span>固定空数组,父组件拒绝更新</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
:expanded-keys="[]"
/>
</HuiSpace>
<HuiSpace class="expanded-bound" direction="vertical">
<span>双向绑定</span>
<HuiOrganizationChart
:data="expandedData"
:data-parent="expandedRedraw"
collapsible
:default-expand-level="0"
v-model:expanded-keys="expandedBound"
/>
</HuiSpace>
<span class="expanded-status">监听请求:{{ expandedLast }};绑定展开:{{ expandedBound }}</span>
</HuiSpace>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| data | OrganizationChartNodeData | OrganizationChartNodeData[] | null | — | 数据:一个根节点,或者多个根节点组成的数组(并排显示)。null、undefined 时什么都不显示 |
| props | OrganizationChartFieldNames | {} | 自定义节点数据的字段名:key(默认 'id')、label、children、disabled |
| direction | 'vertical' | 'horizontal' | 'vertical' | 排列方向:竖向(上级在上)、横向(上级在左) |
| collapsible | boolean | false | 是否可以折叠。开了以后有下级的节点显示展开、收起按钮;不开时始终全部展开 |
| defaultExpandLevel | number | — | 默认展开层级,根为第1层。配置仅在创建时读取,各节点首次出现下级时初始化;未传且无defaultExpandedKeys时全部展开。 |
| defaultExpandedKeys | OrganizationChartKey[] | — | 创建时复制的默认节点key;节点首次出现下级时连上级一起初始化。与defaultExpandLevel同时传时取两者并集。 |
| expandedKeys | OrganizationChartKey[] | — | 支持v-model:expanded-keys;传入即受控,含undefined为空。未传或仅监听时保存内部展开,默认配置不覆盖父实际值。 |
| selectionMode | 'single' | 'multiple' | — | 选择模式:单选、多选。不传时节点不能选中 |
| selection | OrganizationChartSelection | — | 选中的节点,支持 v-model:selection。单选时是一个 key 或 null,多选时是 key 数组 ;未传或仅监听时自持,显式undefined/null受控为空 |
| nodeClass | OrganizationChartClassValue | ((node: OrganizationChartNodeData, level: number) => OrganizationChartClassValue) | — | 节点卡片的类名。传函数时按节点返回,level 根节点是 0 |
Events
事件声明和脚本监听使用驼峰名称;模板仍可写@node-click、@node-expand、@node-collapse、@selection-change。参数对象和发出顺序保持不变。
| 事件 | 回调参数 | 说明 |
|---|---|---|
| nodeClick | (data: { node: OrganizationChartNodeData; key: OrganizationChartKey; level: number }) | 点击卡片、按回车或空格时触发。点展开按钮、点禁用的节点不触发 |
| nodeExpand | (data: { node: OrganizationChartNodeData; key: OrganizationChartKey }) | 点按钮、按键展开节点时触发;调用方法时不触发 |
| nodeCollapse | (data: { node: OrganizationChartNodeData; key: OrganizationChartKey }) | 点按钮、按键收起节点时触发;调用方法时不触发 |
| selectionChange | (data: { selection: OrganizationChartSelection; nodes: OrganizationChartNodeData[] }) | 点击、按键改变选中时触发。nodes 是选中的节点数据,顺序和 selection 一致 |
| update:selection | (value: OrganizationChartSelection) | 选中的值变化时触发,v-model:selection 用 |
| update:expandedKeys | (keys: OrganizationChartKey[]) | 展开的节点变化时触发(点按钮、按键、调用方法都会触发),v-model:expanded-keys 用 |
Slots
| 插槽 | 作用域参数 | 说明 |
|---|---|---|
| default | { node: OrganizationChartNodeData, level: number, expanded: boolean, selected: boolean, disabled: boolean } | 自定义卡片里的内容,不写时显示节点文字。level 根节点是 0 |
方法
通过组件的 ref 调用。
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
| expand | (key: OrganizationChartKey) | void | 展开节点,它的上级也会展开 |
| collapse | (key: OrganizationChartKey) | void | 收起节点 |
| expandAll | — | void | 展开全部节点 |
| collapseAll | — | void | 收起全部节点,只显示根节点 |
| getExpandedKeys | — | OrganizationChartKey[] | 当前展开着的、有下级的节点 key。不开 collapsible 时是全部有下级的节点 |
| focus | (key?: OrganizationChartKey) | void | 让节点获得焦点,并把卡片滚到能看见的位置;被收起看不到时先展开它的上级。不传时聚焦 Tab 停留的节点 |
类型
import type {
OrganizationChartKey,
OrganizationChartDirection,
OrganizationChartSelectionMode,
OrganizationChartSelection,
OrganizationChartNodeData,
OrganizationChartFieldNames,
OrganizationChartClassValue,
OrganizationChartProps,
OrganizationChartSlotProps,
OrganizationChartNodeClickPayload,
OrganizationChartNodeTogglePayload,
OrganizationChartSelectionChangePayload,
OrganizationChartExpose
} from '@nscode/handy-ui'
type OrganizationChartKey = string | number
/** 单选时是一个 key(没选中时是 null),多选时是 key 数组 */
type OrganizationChartSelection = OrganizationChartKey | OrganizationChartKey[] | null
interface OrganizationChartNodeData {
id?: OrganizationChartKey
label?: string
children?: OrganizationChartNodeData[]
disabled?: boolean
/** 可以带任意其他字段,自定义节点内容时能用到 */
[key: string]: unknown
}
/** props 属性的结构:数据里对应的字段名 */
interface OrganizationChartFieldNames {
key?: string
label?: string
children?: string
disabled?: string
}