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:點卡片、或者聚焦節點後按 Enter、空白鍵時觸發,參數還有層級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 }) | 點擊卡片、按 Enter 或空白鍵時觸發。點展開按鈕、點禁用的節點不觸發 |
| 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
}