需求要,但UI没这功能 上代码
<a-tree
:autoExpandParent="false" //加上
:treeData="treeData"
:expandedKeys.sync="expandedKeys"
@expand="onExpand"
:showIcon="false"
>
//自定义树 可不加(主要是客户需要点击内容也要收起展开)
<div
slot="action"
slot-scope="record"
@click="clickTree(record.key, record)"
>
函数
onExpand(a, b) {
// b 就是选中的node对象 很复杂
var key = b.node.eventKey
var record = b.node.dataRef
this.clickTree(key, record)
// 巨坑!!!注意这里的a也就是expandedKeys,但是直接赋值无效,要splice截取全部后添加当前this.expandedKeys
a.splice(0, a.length, ...this.expandedKeys)
return
},
clickTree(key, record) {
const index = this.expandedKeys.indexOf(key)
if (index > -1) {
// 存在
this.expandedKeys.splice(recordKey, 1)
} else {
this.expandedKeys = []
this.expandedKeys.push(key)
this.findDadObj(record)
}
},
loop(data, key, callback) {
data.forEach((item, index, arr) => {
if (item.key === key) {
return callback(item, index, arr)
}
if (item.children) {
return this.loop(item.children, key, callback)
}
})
},
findDadObj(obj) {
// 没父级id就返回
if (!obj.parentId) {
return
}
// 循环总数据 取出父级ID的数据对象,塞入Id并再查找父级
this.loop(this.treeData, obj.parentId, (item) => {
this.expandedKeys.push(item.key)
this.findDadObj(item)
})
},
主要是靠LOOP循环出当前选择的父级ID全部塞到expandedKeys数组里
// 巨坑!
!!注意expand函数的a也就是expandedKeys,直接赋值无效,要splice截取全部后添加当前this.expandedKeys,或者你们用$set试试