Refactor: 树根据新的数据结构的要求重构,支持多级树点击文字选中

This commit is contained in:
Xu Zhimeng 2023-06-19 19:50:04 +08:00
parent 94ae30d3d6
commit e82fee46b7

View File

@ -1,6 +1,7 @@
<template>
<a-tree
class="custom-tree"
ref="treeRef"
v-bind="$attrs"
v-model="checkedKeys"
checkable
@ -14,6 +15,7 @@
</template>
<script>
import { cloneDeep } from 'lodash'
export default {
props: {
treeData: {
@ -41,47 +43,76 @@ export default {
},
methods: {
onTreeSelect(_, { node }) {
const selectedKey = node.eventKey
const parentKey = node.$parent.eventKey
const selectedKey = node.eventKey // key
const findIndex = this.checkedKeys.findIndex(key => key == selectedKey) //
const findIndex = this.checkedKeys.findIndex(key => key == selectedKey)
const children = node.dataRef.children
if (parentKey) {
//
if (children && children.length) {
//
const allChildren = this.getAllChildrenData(node.dataRef.children)
const childrenKeys = allChildren.map(child => child[this.replaceFields.key])
if(node.checked) { //
let _checkedKeys = cloneDeep(this.checkedKeys)
_checkedKeys.splice(findIndex, 1)
_checkedKeys = _checkedKeys.filter(checkedKey => {
return !childrenKeys.includes(checkedKey)
})
this.checkedKeys = _checkedKeys
}
else {
this.checkedKeys.push(selectedKey)
this.checkedKeys.push(...childrenKeys.filter(childKey => !this.checkedKeys.includes(childKey)))
}
} else {
//
const ancestorNodes = this.getAllAncestorNode(node)
if (-1 == findIndex) {
//
this.checkedKeys.push(selectedKey)
const parentNode = this.treeData.find(tree => tree[this.replaceFields.key] == parentKey) //
const childrenKeys = parentNode.children.map(child => child[this.replaceFields.key])
if (childrenKeys.every(key => this.checkedKeys.includes(key))) {
//
this.checkedKeys.push(parentKey)
}
ancestorNodes.forEach(node => {
this.$nextTick(() => {
if (node.checked) {
this.checkedKeys.push(node.eventKey)
}
})
})
} else {
//
this.checkedKeys.splice(findIndex, 1)
const findParent = this.checkedKeys.findIndex(key => key == parentKey)
if (-1 !== findParent) {
this.checkedKeys.splice(findParent, 1)
}
}
} else {
//
const parentNode = this.treeData.find(tree => tree[this.replaceFields.key] == selectedKey)
const childrenKeys = parentNode.children.map(child => child[this.replaceFields.key])
const findParent = this.checkedKeys.findIndex(key => key == selectedKey)
if (-1 == findParent) {
// ,
this.checkedKeys.push(selectedKey)
this.checkedKeys.push(...childrenKeys)
} else {
// ,
this.checkedKeys = this.checkedKeys.filter(key => key !== selectedKey && !childrenKeys.includes(key))
ancestorNodes.forEach(node => { //
const index = this.checkedKeys.findIndex(key => key == node.eventKey)
if(-1 !== index) {
this.checkedKeys.splice(index, 1)
}
})
}
}
},
//
getAllChildrenData(nodeData) {
const childDataList = []
nodeData.forEach(child => {
childDataList.push(child)
if(child.children) {
childDataList.push(...this.getAllChildrenData(child.children))
}
})
return childDataList
},
getAllAncestorNode(currNode) {
const nodeList = []
const parentNode = currNode.$parent
const parentKey = parentNode ? parentNode.eventKey : undefined
const hasParentKey = parentKey !== null && parentKey !== undefined
const isLeafNode = -1 == this.treeData.findIndex(treeNode => treeNode[this.replaceFields.key] == parentKey)
nodeList.push(parentNode)
if (hasParentKey && isLeafNode) {
nodeList.push(...this.getAllAncestorNode(parentNode))
}
return nodeList
}
},
watch: {