<template>
<div>
<div class="left">
<Tree :data="data"></Tree>
</div>
</div>
</template>
<script>
import Tree from '@/components/tree/index.vue'
import {GetOrganiztionalData,GetUserList} from '@/api/detailedList'
export default {
components: {Tree},
data() {
return {
data: []
}
},
props: {
treeUserInfo: Object
},
created() {
this.getOrganizational()
},
methods: {
// 辅助函数:根据部门ID找到对应的部门节点
findDepartmentNode(departmentId, nodes) {
for (const node of nodes) {
if (node.id === departmentId) {
return node;
}
// 如果该节点有子节点,递归查找
if (node.children && node.children.length > 0) {
const foundNode = this.findDepartmentNode(departmentId, node.children);
if (foundNode) {
return foundNode;
}
}
}
return null; // 如果找不到对应的部门节点
},
getOrganizational() {
GetOrganiztionalData({}).then(res => {
if(this.treeUserInfo.erpCode) {
console.log('2222')
this.data = [res.data]
const id = this.treeUserInfo.erpDepCode
this.getUserInfo(id)
} else {
this.data = [res.data]
}
})
},
getUserInfo(id) {
const departmentNode = this.findDepartmentNode(id, this.data)
if(!departmentNode.children) {
this.$set(departmentNode, 'children', [])
}
const obj = {
erpParentCode: id
}
let array = []
GetUserList(obj).then(res => {
res.data.map(item => {
array.push({
text:item.erpName,
erpCode: item.erpCode
})
})
departmentNode.children.push(...array)
})
}
},
}
</script>
这是我写的代码,走console.log('222')的时候,传递给子组件的 data数据没有,应该怎么解决
子组件代码
<template>
<div>
<el-tree :data="data" :node-key="data.id" default-expand-all :expand-on-click-node="false" ref="tree" :default-checked-keys="defaultCheckKeys">
<span slot-scope="{node,data}">
<div>
<!-- 没有子级的展开图标 -->
<i v-if="!data.children" class="el-icon-folder" @click.stop="handleExpandClick(node,data)"></i>
<i v-else-if="node.expanded" class="el-icon-folder-opened" @click.stop="handleExpandClick(node,data)"></i>
<i v-else class="el-icon-folder" @click.stop="handleExpandClick(node,data)"></i>
</div>
</span>
</el-tree>
</div>
</template>
<script>
export default {
data() {
return {
defaultCheckKeys: []
}
},
props: {
data: Array,
keys:String
},
methods: {
handleExpandClick(node,data) {
node.expanded = !node.expanded;
}
}
</script>