<el-tree
:data="data"
:props="defaultProps"
@node-click="handleNodeClick"
/>
div>
data() {
return {
data: [
{
label: '一级 1',
children: [
{
label: '二级 1-1',
children: [
{
label: '三级 1-1-1'
}
]
}
]
}
],
defaultProps: {
label: 'label',
children: 'children'
}
}
},
methods: {
handleNodeClick(data) {
// 获取当前数据对象,
// 比如 点击最后一项 输出 “{ label: '三级 1-1-1' }”
console.log(data)
}
}
注意:defaultProps 的默认值为 {label: 'label',children: 'children'}
时可忽略写。
下面案例中,实现复选框的同时,也展示了动态加载节点数据的方法:
<el-tree
:props="defaultProps"
:load="loadNode"
lazy
show-checkbox
@check-change="handleCheckChange"
/>
data() {
return {
defaultProps: {
label: 'name',
children: 'zones'
}
}
},
methods: {
loadNode(node, resolve) {
// 往1级添加数据
if (node.level === 0) {
return resolve([{ name: '福建省' }, { name: '广东省' }])
}
// 第四级(最后一级)就不用加载效果了
if (node.level > 3) return resolve([])
// 接下来,实现动态加载节点数据
var hasChild
if (node.data.name === '福建省') {
hasChild = true
} else if (node.data.name === '广东省') {
hasChild = false
} else {
hasChild = Math.random() > 0.5 // 其余省份的子元素就随机存在
}
// 添加子元素进内部
setTimeout(() => {
var data
if (hasChild) {
data = [
{ name: '厦门市' }
]
} else {
data = []
}
resolve(data)
}, 500)
},
handleCheckChange(data, checked, indeterminate) {
// data:原数据
// checked:选中状态
// indeterminate:原始选中状态
console.log(data, checked, indeterminate)
}
}
<el-tree
:props="defaultProps"
:load="loadNode"
lazy
show-checkbox
/>
data() {
return {
defaultProps: {
label: 'name',
children: 'zones',
isLeaf: 'leaf'
}
}
},
methods: {
loadNode(node, resolve) {
// 往1级添加数据
if (node.level === 0) {
return resolve([{ name: '福建省' }, { name: '广东省' }])
}
// 第2级(最后一级)就不用加载效果了
if (node.level > 1) return resolve([])
// 添加子元素进内部
setTimeout(() => {
const data = [
{ name: '厦门市', leaf: 'true' }
]
resolve(data)
}, 500)
}
}
default-expanded-keys
设置默认展开的节点;
default-checked-keys
设置默认选中的节点。
注意:此时必须设置 node-key
,其值为节点数据中的一个字段名,在整棵树中是唯一的。
<el-tree
:data="data"
show-checkbox
node-key="id"
:default-checked-keys="[3]"
:default-expanded-keys="[1]"
/>
data() {
return {
data: [
{
id: 1,
label: '一级',
children: [
{
id: 2,
label: '二级',
children: [
{
id: 3,
label: '三级'
}
]
}
]
}
]
}
},
在每一项,添加 disabled: true
,就可以实现了
data: [
{
id: 1,
label: '一级',
children: [
{
id: 2,
label: '二级',
children: [
{
id: 3,
label: '三级',
disabled: true
}
]
}
]
}
]
<el-tree
ref="tree"
:data="data"
show-checkbox
default-expand-all
node-key="id"
highlight-current
/>
<div class="buttons">
<el-button @click="getCheckedNodes">通过 node 获取el-button>
<el-button @click="setCheckedNodes">通过 node 设置el-button>
<el-button @click="getCheckedKeys">通过 key 获取el-button>
<el-button @click="setCheckedKeys">通过 key 设置el-button>
<el-button @click="resetChecked">清空el-button>
div>
data() {
return {
data: [
{
id: 1,
label: '一级',
children: [
{
id: 2,
label: '二级',
children: [
{
id: 3,
label: '三级'
}
]
}
]
}
]
}
},
methods: {
/* ⭐基于数据内容 */
// 获取
getCheckedNodes() {
console.log(this.$refs.tree.getCheckedNodes())
},
// 回填
setCheckedNodes() {
this.$refs.tree.setCheckedNodes([{ id: 2, label: '二级' }])
},
/* ⭐基于 key,这里指 id */
// 获取
getCheckedKeys() {
console.log(this.$refs.tree.getCheckedKeys()) // 输出: [1,2,3]
},
// 回填
setCheckedKeys() {
this.$refs.tree.setCheckedKeys([3])
},
// 清空选中
resetChecked() {
this.$refs.tree.setCheckedKeys([])
}
}
<el-tree
:data="data"
show-checkbox
node-key="id"
default-expand-all
:expand-on-click-node="false"
>
<span slot-scope="{node}" class="custom-tree-node">
<span>{{ node.label }}span>
<span>
<el-button
type="text"
size="mini"
@click="() => append(data)"
>添加el-button>
<el-button
type="text"
size="mini"
@click="() => remove(node, data)"
>删除el-button>
span>
span>
el-tree>
<el-input
v-model="filterText"
placeholder="输入关键字进行过滤"
/>
<el-tree
ref="tree"
:data="data"
default-expand-all
:filter-node-method="filterNode"
/>
data() {
return {
filterText: '',
data: [
{
id: 1,
label: '一级',
children: [
{
id: 2,
label: '二级',
children: [
{
id: 3,
label: '三级'
}
]
}
]
}
]
}
},
watch: {
filterText(val) {
this.$refs.tree.filter(val)
}
},
methods: {
filterNode(value, data) {
if (!value) return true
return data.label.indexOf(value) !== -1
}
}
作用:对于同一级的节点,每次只能展开一个
实现:直接在 el-tree
添加属性 accordion
<el-tree
accordion
/>
通过 draggable 属性可让节点变为可拖拽。
<el-tree
:data="data"
node-key="id"
default-expand-all
draggable
:allow-drop="allowDrop"
:allow-drag="allowDrag"
@node-drag-start="handleDragStart"
@node-drag-enter="handleDragEnter"
@node-drag-leave="handleDragLeave"
@node-drag-over="handleDragOver"
@node-drag-end="handleDragEnd"
@node-drop="handleDrop"
/>
data() {
return {
filterText: '',
data: [
{
id: 1,
label: '一级',
children: [
{
id: 2,
label: '二级',
children: [
{
id: 3,
label: '三级'
}
]
}
]
}
]
}
},
methods: {
handleDragStart(node, ev) {
console.log('drag start', node)
},
handleDragEnter(draggingNode, dropNode, ev) {
console.log('tree drag enter: ', dropNode.label)
},
handleDragLeave(draggingNode, dropNode, ev) {
console.log('tree drag leave: ', dropNode.label)
},
handleDragOver(draggingNode, dropNode, ev) {
console.log('tree drag over: ', dropNode.label)
},
handleDragEnd(draggingNode, dropNode, dropType, ev) {
console.log('tree drag end: ', dropNode && dropNode.label, dropType)
},
handleDrop(draggingNode, dropNode, dropType, ev) {
console.log('tree drop: ', dropNode.label, dropType)
},
allowDrop(draggingNode, dropNode, type) {
if (dropNode.data.label === '二级 3-1') {
return type !== 'inner'
} else {
return true
}
},
allowDrag(draggingNode) {
return draggingNode.data.label.indexOf('三级 3-2-2') === -1
}
}
以下举例:只想获取一级和二级的节点key集合
getCheckedKeys() {
const allCheckedKeys = this.$refs.tree.getCheckedKeys()
const filteredKeys = allCheckedKeys.filter(key => {
// 判断是否是一级或二级节点
const node = this.$refs.tree.getNode(key)
return node.level === 1 || node.level === 2
})
console.log(filteredKeys) // 输出: [1, 2]
},