<!-- html模版 -->
<el-card class="cardHeight">
<template #header>
全选/全不选:
<el-switch
v-model="treeNodeAll"
active-text="是"
inactive-text="否"
inline-prompt
@change="handleCheckedTreeNodeAll"
/>
全部展开/折叠:
<el-switch
v-model="menuExpand"
active-text="展开"
inactive-text="折叠"
inline-prompt
@change="handleCheckedTreeExpand"
/>
</template>
<!-- :load="loadNode"lazy -->
<el-tree
:default-expand-all="isExpand"
ref="treeRef"
:data="menuOptions"
:props="defaultProps"
empty-text="加载中,请稍候"
node-key="id"
show-checkbox
/>
</el-card>
// ts
/** 全选/全不选 */
const handleCheckedTreeNodeAll = () => {
treeRef.value.setCheckedNodes(treeNodeAll.value ? menuOptions.value : [])
}
const recursionFn = (arr: any) => {
const nodes = treeRef.value?.store?.nodesMap
if (arr.length > 0) {
for (let i = 0; i < arr.length; i++) {
if (arr[i]?.children?.length > 0) {
nodes[arr[i].id].expanded = isExpand.value
recursionFn(arr[i]?.children)
}
}
}
}
//一键展开折叠的第一种做法(一次性数据返回时及数据量大的状态下,操作效果较快)
/** 展开/折叠全部 */
const isExpand = ref(false)
const handleCheckedTreeExpand = () => {
isExpand.value = !isExpand.value
// 后端返回的数据
let treeList = menuOptions.value
recursionFn(treeList)
}
//一键展开折叠的第二种做法(一次性数据返回时及数据量大的状态下,操作效果一般)
const handleCheckedTreeExpand = (type: any) => {
const nodes = treeRef.value?.store?.nodesMap
if (type) {
menuExpand.value = true
Object.values(nodes).forEach((v: any) => v.expand())
} else {
menuExpand.value = false
Object.values(nodes).map((v: any) => v.collapse())
}
}
//一键展开折叠的第三种做法(一次性数据返回时及数据量大的状态下,操作效果一般)
const handleCheckedTreeExpand = () => {
const nodes = treeRef.value?.store?.nodesMap
console.log('treeRef', treeRef.value)
for (let node in nodes) {
if (nodes[node].expanded === menuExpand.value) {
continue
}
nodes[node].expanded = menuExpand.value
}
}