用清晰的層級結構展示信息,可展開或折疊。
基礎用法
基礎的樹形結構展示。

1 <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree> 2 3 <script> 4 export default { 5 data() { 6 return { 7 data: [{ 8 label: '一級 1', 9 children: [{ 10 label: '二級 1-1', 11 children: [{ 12 label: '三級 1-1-1' 13 }] 14 }] 15 }, { 16 label: '一級 2', 17 children: [{ 18 label: '二級 2-1', 19 children: [{ 20 label: '三級 2-1-1' 21 }] 22 }, { 23 label: '二級 2-2', 24 children: [{ 25 label: '三級 2-2-1' 26 }] 27 }] 28 }, { 29 label: '一級 3', 30 children: [{ 31 label: '二級 3-1', 32 children: [{ 33 label: '三級 3-1-1' 34 }] 35 }, { 36 label: '二級 3-2', 37 children: [{ 38 label: '三級 3-2-1' 39 }] 40 }] 41 }], 42 defaultProps: { 43 children: 'children', 44 label: 'label' 45 } 46 }; 47 }, 48 methods: { 49 handleNodeClick(data) { 50 console.log(data); 51 } 52 } 53 }; 54 </script>
可選擇
適用於需要選擇層級時使用。
本例還展示了動態加載節點數據的方法。

1 <el-tree 2 :props="props" 3 :load="loadNode" 4 lazy 5 show-checkbox 6 @check-change="handleCheckChange"> 7 </el-tree> 8 9 <script> 10 export default { 11 data() { 12 return { 13 props: { 14 label: 'name', 15 children: 'zones' 16 }, 17 count: 1 18 }; 19 }, 20 methods: { 21 handleCheckChange(data, checked, indeterminate) { 22 console.log(data, checked, indeterminate); 23 }, 24 handleNodeClick(data) { 25 console.log(data); 26 }, 27 loadNode(node, resolve) { 28 if (node.level === 0) { 29 return resolve([{ name: 'region1' }, { name: 'region2' }]); 30 } 31 if (node.level > 3) return resolve([]); 32 33 var hasChild; 34 if (node.data.name === 'region1') { 35 hasChild = true; 36 } else if (node.data.name === 'region2') { 37 hasChild = false; 38 } else { 39 hasChild = Math.random() > 0.5; 40 } 41 42 setTimeout(() => { 43 var data; 44 if (hasChild) { 45 data = [{ 46 name: 'zone' + this.count++ 47 }, { 48 name: 'zone' + this.count++ 49 }]; 50 } else { 51 data = []; 52 } 53 54 resolve(data); 55 }, 500); 56 } 57 } 58 }; 59 </script>
懶加載自定義葉子節點
由於在點擊節點時才進行該層數據的獲取,默認情況下 Tree 無法預知某個節點是否為葉子節點,所以會為每個節點添加一個下拉按鈕,如果節點沒有下層數據,則點擊后下拉按鈕會消失。同時,你也可以提前告知 Tree 某個節點是否為葉子節點,從而避免在葉子節點前渲染下拉按鈕。

1 <el-tree 2 :props="props1" 3 :load="loadNode1" 4 lazy 5 show-checkbox> 6 </el-tree> 7 8 <script> 9 export default { 10 data() { 11 return { 12 props1: { 13 label: 'name', 14 children: 'zones', 15 isLeaf: 'leaf' 16 }, 17 }; 18 }, 19 methods: { 20 loadNode1(node, resolve) { 21 if (node.level === 0) { 22 return resolve([{ name: 'region' }]); 23 } 24 if (node.level > 1) return resolve([]); 25 26 setTimeout(() => { 27 const data = [{ 28 name: 'leaf', 29 leaf: true 30 }, { 31 name: 'zone' 32 }]; 33 34 resolve(data); 35 }, 500); 36 } 37 } 38 }; 39 </script>
默認展開和默認選中
可將 Tree 的某些節點設置為默認展開或默認選中
分別通過default-expanded-keys
和default-checked-keys
設置默認展開和默認選中的節點。需要注意的是,此時必須設置node-key
,其值為節點數據中的一個字段名,該字段在整棵樹中是唯一的。

1 <el-tree 2 :data="data2" 3 show-checkbox 4 node-key="id" 5 :default-expanded-keys="[2, 3]" 6 :default-checked-keys="[5]" 7 :props="defaultProps"> 8 </el-tree> 9 10 <script> 11 export default { 12 data() { 13 return { 14 data2: [{ 15 id: 1, 16 label: '一級 1', 17 children: [{ 18 id: 4, 19 label: '二級 1-1', 20 children: [{ 21 id: 9, 22 label: '三級 1-1-1' 23 }, { 24 id: 10, 25 label: '三級 1-1-2' 26 }] 27 }] 28 }, { 29 id: 2, 30 label: '一級 2', 31 children: [{ 32 id: 5, 33 label: '二級 2-1' 34 }, { 35 id: 6, 36 label: '二級 2-2' 37 }] 38 }, { 39 id: 3, 40 label: '一級 3', 41 children: [{ 42 id: 7, 43 label: '二級 3-1' 44 }, { 45 id: 8, 46 label: '二級 3-2' 47 }] 48 }], 49 defaultProps: { 50 children: 'children', 51 label: 'label' 52 } 53 }; 54 } 55 }; 56 </script>
禁用狀態
可將 Tree 的某些節點設置為禁用狀態
通過disabled
設置禁用狀態。

1 <el-tree 2 :data="data3" 3 show-checkbox 4 node-key="id" 5 :default-expanded-keys="[2, 3]" 6 :default-checked-keys="[5]"> 7 </el-tree> 8 9 <script> 10 export default { 11 data() { 12 return { 13 data3: [{ 14 id: 1, 15 label: '一級 2', 16 children: [{ 17 id: 3, 18 label: '二級 2-1', 19 children: [{ 20 id: 4, 21 label: '三級 3-1-1' 22 }, { 23 id: 5, 24 label: '三級 3-1-2', 25 disabled: true 26 }] 27 }, { 28 id: 2, 29 label: '二級 2-2', 30 disabled: true, 31 children: [{ 32 id: 6, 33 label: '三級 3-2-1' 34 }, { 35 id: 7, 36 label: '三級 3-2-2', 37 disabled: true 38 }] 39 }] 40 }], 41 defaultProps: { 42 children: 'children', 43 label: 'label' 44 } 45 }; 46 } 47 }; 48 </script>
樹節點的選擇
本例展示如何獲取和設置選中節點。獲取和設置各有兩種方式:通過 node 或通過 key。如果需要通過 key 來獲取或設置,則必須設置node-key
。

1 <el-tree 2 :data="data2" 3 show-checkbox 4 default-expand-all 5 node-key="id" 6 ref="tree" 7 highlight-current 8 :props="defaultProps"> 9 </el-tree> 10 11 <div class="buttons"> 12 <el-button @click="getCheckedNodes">通過 node 獲取</el-button> 13 <el-button @click="getCheckedKeys">通過 key 獲取</el-button> 14 <el-button @click="setCheckedNodes">通過 node 設置</el-button> 15 <el-button @click="setCheckedKeys">通過 key 設置</el-button> 16 <el-button @click="resetChecked">清空</el-button> 17 </div> 18 19 <script> 20 export default { 21 methods: { 22 getCheckedNodes() { 23 console.log(this.$refs.tree.getCheckedNodes()); 24 }, 25 getCheckedKeys() { 26 console.log(this.$refs.tree.getCheckedKeys()); 27 }, 28 setCheckedNodes() { 29 this.$refs.tree.setCheckedNodes([{ 30 id: 5, 31 label: '二級 2-1' 32 }, { 33 id: 9, 34 label: '三級 1-1-1' 35 }]); 36 }, 37 setCheckedKeys() { 38 this.$refs.tree.setCheckedKeys([3]); 39 }, 40 resetChecked() { 41 this.$refs.tree.setCheckedKeys([]); 42 } 43 }, 44 45 data() { 46 return { 47 data2: [{ 48 id: 1, 49 label: '一級 1', 50 children: [{ 51 id: 4, 52 label: '二級 1-1', 53 children: [{ 54 id: 9, 55 label: '三級 1-1-1' 56 }, { 57 id: 10, 58 label: '三級 1-1-2' 59 }] 60 }] 61 }, { 62 id: 2, 63 label: '一級 2', 64 children: [{ 65 id: 5, 66 label: '二級 2-1' 67 }, { 68 id: 6, 69 label: '二級 2-2' 70 }] 71 }, { 72 id: 3, 73 label: '一級 3', 74 children: [{ 75 id: 7, 76 label: '二級 3-1' 77 }, { 78 id: 8, 79 label: '二級 3-2' 80 }] 81 }], 82 defaultProps: { 83 children: 'children', 84 label: 'label' 85 } 86 }; 87 } 88 }; 89 </script>
自定義節點內容
節點的內容支持自定義,可以在節點區添加按鈕或圖標等內容
可以通過兩種方法進行樹節點內容的自定義:render-content
和 scoped slot。使用render-content
指定渲染函數,該函數返回需要的節點區內容即可。渲染函數的用法請參考 Vue 文檔。使用 scoped slot 會傳入兩個參數node
和data
,分別表示當前節點的 Node 對象和當前節點的數據。注意:由於 jsfiddle 不支持 JSX 語法,所以render-content
示例在 jsfiddle 中無法運行。但是在實際的項目中,只要正確地配置了相關依賴,就可以正常運行。

1 <div class="custom-tree-container"> 2 <div class="block"> 3 <p>使用 render-content</p> 4 <el-tree 5 :data="data4" 6 show-checkbox 7 node-key="id" 8 default-expand-all 9 :expand-on-click-node="false" 10 :render-content="renderContent"> 11 </el-tree> 12 </div> 13 <div class="block"> 14 <p>使用 scoped slot</p> 15 <el-tree 16 :data="data5" 17 show-checkbox 18 node-key="id" 19 default-expand-all 20 :expand-on-click-node="false"> 21 <span class="custom-tree-node" slot-scope="{ node, data }"> 22 <span>{{ node.label }}</span> 23 <span> 24 <el-button 25 type="text" 26 size="mini" 27 @click="() => append(data)"> 28 Append 29 </el-button> 30 <el-button 31 type="text" 32 size="mini" 33 @click="() => remove(node, data)"> 34 Delete 35 </el-button> 36 </span> 37 </span> 38 </el-tree> 39 </div> 40 </div> 41 42 <script> 43 let id = 1000; 44 45 export default { 46 data() { 47 const data = [{ 48 id: 1, 49 label: '一級 1', 50 children: [{ 51 id: 4, 52 label: '二級 1-1', 53 children: [{ 54 id: 9, 55 label: '三級 1-1-1' 56 }, { 57 id: 10, 58 label: '三級 1-1-2' 59 }] 60 }] 61 }, { 62 id: 2, 63 label: '一級 2', 64 children: [{ 65 id: 5, 66 label: '二級 2-1' 67 }, { 68 id: 6, 69 label: '二級 2-2' 70 }] 71 }, { 72 id: 3, 73 label: '一級 3', 74 children: [{ 75 id: 7, 76 label: '二級 3-1' 77 }, { 78 id: 8, 79 label: '二級 3-2' 80 }] 81 }]; 82 return { 83 data4: JSON.parse(JSON.stringify(data)), 84 data5: JSON.parse(JSON.stringify(data)) 85 } 86 }, 87 88 methods: { 89 append(data) { 90 const newChild = { id: id++, label: 'testtest', children: [] }; 91 if (!data.children) { 92 this.$set(data, 'children', []); 93 } 94 data.children.push(newChild); 95 }, 96 97 remove(node, data) { 98 const parent = node.parent; 99 const children = parent.data.children || parent.data; 100 const index = children.findIndex(d => d.id === data.id); 101 children.splice(index, 1); 102 }, 103 104 renderContent(h, { node, data, store }) { 105 return ( 106 <span class="custom-tree-node"> 107 <span>{node.label}</span> 108 <span> 109 <el-button size="mini" type="text" on-click={ () => this.append(data) }>Append</el-button> 110 <el-button size="mini" type="text" on-click={ () => this.remove(node, data) }>Delete</el-button> 111 </span> 112 </span>); 113 } 114 } 115 }; 116 </script> 117 118 <style> 119 .custom-tree-node { 120 flex: 1; 121 display: flex; 122 align-items: center; 123 justify-content: space-between; 124 font-size: 14px; 125 padding-right: 8px; 126 } 127 </style>
節點過濾
通過關鍵字過濾樹節點
在需要對節點進行過濾時,調用 Tree 實例的filter
方法,參數為關鍵字。需要注意的是,此時需要設置filter-node-method
,值為過濾函數。

1 <el-input 2 placeholder="輸入關鍵字進行過濾" 3 v-model="filterText"> 4 </el-input> 5 6 <el-tree 7 class="filter-tree" 8 :data="data2" 9 :props="defaultProps" 10 default-expand-all 11 :filter-node-method="filterNode" 12 ref="tree2"> 13 </el-tree> 14 15 <script> 16 export default { 17 watch: { 18 filterText(val) { 19 this.$refs.tree2.filter(val); 20 } 21 }, 22 23 methods: { 24 filterNode(value, data) { 25 if (!value) return true; 26 return data.label.indexOf(value) !== -1; 27 } 28 }, 29 30 data() { 31 return { 32 filterText: '', 33 data2: [{ 34 id: 1, 35 label: '一級 1', 36 children: [{ 37 id: 4, 38 label: '二級 1-1', 39 children: [{ 40 id: 9, 41 label: '三級 1-1-1' 42 }, { 43 id: 10, 44 label: '三級 1-1-2' 45 }] 46 }] 47 }, { 48 id: 2, 49 label: '一級 2', 50 children: [{ 51 id: 5, 52 label: '二級 2-1' 53 }, { 54 id: 6, 55 label: '二級 2-2' 56 }] 57 }, { 58 id: 3, 59 label: '一級 3', 60 children: [{ 61 id: 7, 62 label: '二級 3-1' 63 }, { 64 id: 8, 65 label: '二級 3-2' 66 }] 67 }], 68 defaultProps: { 69 children: 'children', 70 label: 'label' 71 } 72 }; 73 } 74 }; 75 </script>
手風琴模式
對於同一級的節點,每次只能展開一個

1 <el-tree 2 :data="data" 3 :props="defaultProps" 4 accordion 5 @node-click="handleNodeClick"> 6 </el-tree> 7 8 <script> 9 export default { 10 data() { 11 return { 12 data: [{ 13 label: '一級 1', 14 children: [{ 15 label: '二級 1-1', 16 children: [{ 17 label: '三級 1-1-1' 18 }] 19 }] 20 }, { 21 label: '一級 2', 22 children: [{ 23 label: '二級 2-1', 24 children: [{ 25 label: '三級 2-1-1' 26 }] 27 }, { 28 label: '二級 2-2', 29 children: [{ 30 label: '三級 2-2-1' 31 }] 32 }] 33 }, { 34 label: '一級 3', 35 children: [{ 36 label: '二級 3-1', 37 children: [{ 38 label: '三級 3-1-1' 39 }] 40 }, { 41 label: '二級 3-2', 42 children: [{ 43 label: '三級 3-2-1' 44 }] 45 }] 46 }], 47 defaultProps: { 48 children: 'children', 49 label: 'label' 50 } 51 }; 52 }, 53 methods: { 54 handleNodeClick(data) { 55 console.log(data); 56 } 57 } 58 }; 59 </script>
Attributes
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
data | 展示數據 | array | — | — |
empty-text | 內容為空的時候展示的文本 | String | — | — |
node-key | 每個樹節點用來作為唯一標識的屬性,整棵樹應該是唯一的 | String | — | — |
props | 配置選項,具體看下表 | object | — | — |
render-after-expand | 是否在第一次展開某個樹節點后才渲染其子節點 | boolean | — | true |
load | 加載子樹數據的方法,僅當 lazy 屬性為true 時生效 | function(node, resolve) | — | — |
render-content | 樹節點的內容區的渲染 Function | Function(h, { node, data, store } | — | — |
highlight-current | 是否高亮當前選中節點,默認值是 false。 | boolean | — | false |
default-expand-all | 是否默認展開所有節點 | boolean | — | false |
expand-on-click-node | 是否在點擊節點的時候展開或者收縮節點, 默認值為 true,如果為 false,則只有點箭頭圖標的時候才會展開或者收縮節點。 | boolean | — | true |
auto-expand-parent | 展開子節點的時候是否自動展開父節點 | boolean | — | true |
default-expanded-keys | 默認展開的節點的 key 的數組 | array | — | — |
show-checkbox | 節點是否可被選擇 | boolean | — | false |
check-strictly | 在顯示復選框的情況下,是否嚴格的遵循父子不互相關聯的做法,默認為 false | boolean | — | false |
default-checked-keys | 默認勾選的節點的 key 的數組 | array | — | — |
filter-node-method | 對樹節點進行篩選時執行的方法,返回 true 表示這個節點可以顯示,返回 false 則表示這個節點會被隱藏 | Function(value, data, node) | — | — |
accordion | 是否每次只打開一個同級樹節點展開 | boolean | — | false |
indent | 相鄰級節點間的水平縮進,單位為像素 | number | — | 16 |
lazy | 是否懶加載子節點,需與 load 方法結合使用 | boolean | — | false |
props
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
label | 指定節點標簽為節點對象的某個屬性值 | string, function(data, node) | — | — |
children | 指定子樹為節點對象的某個屬性值 | string | — | — |
disabled | 指定節點選擇框是否禁用為節點對象的某個屬性值 | boolean, function(data, node) | — | — |
isLeaf | 指定節點是否為葉子節點,僅在指定了 lazy 屬性的情況下生效 | boolean, function(data, node) | — | — |
方法
Tree
內部使用了 Node 類型的對象來包裝用戶傳入的數據,用來保存目前節點的狀態。 Tree
擁有如下方法:
方法名 | 說明 | 參數 |
---|---|---|
filter | 對樹節點進行篩選操作 | 接收一個任意類型的參數,該參數會在 filter-node-method 中作為第一個參數 |
updateKeyChildren | 通過 keys 設置節點子元素,使用此方法必須設置 node-key 屬性 | (key, data) 接收兩個參數,1. 節點 key 2. 節點數據的數組 |
getCheckedNodes | 若節點可被選擇(即 show-checkbox 為 true ),則返回目前被選中的節點所組成的數組 |
(leafOnly) 接收一個 boolean 類型的參數,若為 true 則僅返回被選中的葉子節點,默認值為 false |
setCheckedNodes | 設置目前勾選的節點,使用此方法必須設置 node-key 屬性 | (nodes) 接收勾選節點數據的數組 |
getCheckedKeys | 若節點可被選擇(即 show-checkbox 為 true ),則返回目前被選中的節點的 key 所組成的數組 |
(leafOnly) 接收一個 boolean 類型的參數,若為 true 則僅返回被選中的葉子節點的 keys,默認值為 false |
setCheckedKeys | 通過 keys 設置目前勾選的節點,使用此方法必須設置 node-key 屬性 | (keys, leafOnly) 接收兩個參數,1. 勾選節點的 key 的數組 2. boolean 類型的參數,若為 true 則僅設置葉子節點的選中狀態,默認值為 false |
setChecked | 通過 key / data 設置某個節點的勾選狀態,使用此方法必須設置 node-key 屬性 | (key/data, checked, deep) 接收三個參數,1. 勾選節點的 key 或者 data 2. boolean 類型,節點是否選中 3. boolean 類型,是否設置子節點 ,默認為 false |
getHalfCheckedNodes | 若節點可被選擇(即 show-checkbox 為 true ),則返回目前半選中的節點所組成的數組 |
- |
getHalfCheckedKeys | 若節點可被選擇(即 show-checkbox 為 true ),則返回目前半選中的節點的 key 所組成的數組 |
- |
getCurrentKey | 獲取當前被選中節點的 key,使用此方法必須設置 node-key 屬性,若沒有節點被選中則返回 null | — |
getCurrentNode | 獲取當前被選中節點的 node,若沒有節點被選中則返回 null | — |
setCurrentKey | 通過 key 設置某個節點的當前選中狀態,使用此方法必須設置 node-key 屬性 | (key) 待被選節點的 key |
setCurrentNode | 通過 node 設置某個節點的當前選中狀態,使用此方法必須設置 node-key 屬性 | (node) 待被選節點的 node |
getNode | 根據 data 或者 key 拿到 Tree 組件中的 node | (data) 要獲得 node 的 key 或者 data |
remove | 刪除 Tree 中的一個節點 | (data) 要刪除的節點的 data、key 或者 node |
append | 為 Tree 中的一個節點追加一個子節點 | (data, parentNode) 接收兩個參數,1. 要追加的子節點的 data 2. 子節點的 parent 的 data、key 或者 node |
insertBefore | 為 Tree 的一個節點的前面增加一個節點 | (data, refNode) 接收兩個參數,1. 要增加的節點的 data 2. 要增加的節點的后一個節點的 data、key 或者 node |
insertAfter | 為 Tree 的一個節點的后面增加一個節點 | (data, refNode) 接收兩個參數,1. 要增加的節點的 data 2. 要增加的節點的前一個節點的 data、key 或者 node |
Events
事件名稱 | 說明 | 回調參數 |
---|---|---|
node-click | 節點被點擊時的回調 | 共三個參數,依次為:傳遞給 data 屬性的數組中該節點所對應的對象、節點對應的 Node、節點組件本身。 |
node-contextmenu | 當某一節點被鼠標右鍵點擊時會觸發該事件 | 共四個參數,依次為:event、傳遞給 data 屬性的數組中該節點所對應的對象、節點對應的 Node、節點組件本身。 |
check-change | 節點選中狀態發生變化時的回調 | 共三個參數,依次為:傳遞給 data 屬性的數組中該節點所對應的對象、節點本身是否被選中、節點的子樹中是否有被選中的節點 |
check | 當復選框被點擊的時候觸發 | 共兩個參數,依次為:傳遞給 data 屬性的數組中該節點所對應的對象、樹目前的選中狀態對象,包含 checkedNodes、checkedKeys、halfCheckedNodes、halfCheckedKeys 四個屬性 |
current-change | 當前選中節點變化時觸發的事件 | 共兩個參數,依次為:當前節點的數據,當前節點的 Node 對象 |
node-expand | 節點被展開時觸發的事件 | 共三個參數,依次為:傳遞給 data 屬性的數組中該節點所對應的對象、節點對應的 Node、節點組件本身。 |
node-collapse | 節點被關閉時觸發的事件 | 共三個參數,依次為:傳遞給 data 屬性的數組中該節點所對應的對象、節點對應的 Node、節點組件本身。 |
Scoped slot
name | 說明 |
---|---|
— | 自定義樹節點的內容,參數為 { node, data } |