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> <template>
<a-tree <a-tree
class="custom-tree" class="custom-tree"
ref="treeRef"
v-bind="$attrs" v-bind="$attrs"
v-model="checkedKeys" v-model="checkedKeys"
checkable checkable
@ -14,6 +15,7 @@
</template> </template>
<script> <script>
import { cloneDeep } from 'lodash'
export default { export default {
props: { props: {
treeData: { treeData: {
@ -41,47 +43,76 @@ export default {
}, },
methods: { methods: {
onTreeSelect(_, { node }) { onTreeSelect(_, { node }) {
const selectedKey = node.eventKey const selectedKey = node.eventKey // key
const parentKey = node.$parent.eventKey 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) { if (-1 == findIndex) {
// //
this.checkedKeys.push(selectedKey) this.checkedKeys.push(selectedKey)
ancestorNodes.forEach(node => {
const parentNode = this.treeData.find(tree => tree[this.replaceFields.key] == parentKey) // this.$nextTick(() => {
const childrenKeys = parentNode.children.map(child => child[this.replaceFields.key]) if (node.checked) {
if (childrenKeys.every(key => this.checkedKeys.includes(key))) { this.checkedKeys.push(node.eventKey)
// }
this.checkedKeys.push(parentKey) })
} })
} else { } else {
// //
this.checkedKeys.splice(findIndex, 1) this.checkedKeys.splice(findIndex, 1)
ancestorNodes.forEach(node => { //
const findParent = this.checkedKeys.findIndex(key => key == parentKey) const index = this.checkedKeys.findIndex(key => key == node.eventKey)
if (-1 !== findParent) { if(-1 !== index) {
this.checkedKeys.splice(findParent, 1) this.checkedKeys.splice(index, 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))
} }
} }
},
//
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: { watch: {