树形组件
Tree 树形控件 | Element Plus (element-plus.org)
export function transListToTreeData(list, rootValue) {
const arr = []
list.forEach(item => {
if (item.pid === rootValue) {
// 找到了匹配的节点
arr.push(item)
// 当前节点的id 和 当前节点的子节点的pid是想等的
const children = transListToTreeData(list, item.id) // 找到的节点的子节点
if (children.length) { item.children = children } // 将子节点赋值给当前节点
}
})
return arr
}
添加子部门
弹层
methods: {
// 封装好方法
async getDepartment() {
const result = await getDepartment()
this.depts = transListToTreeData(result, 0)
},
// 操作部门方法
operateDept(type, id) {
if (type === 'add') {
// 添加子部门
this.showDialog = true // 显示弹层
this.currentNodeId = id
} else if (type === 'edit') {
// 编辑部门场景
this.showDialog = true
this.currentNodeId = id // 记录id 要用它获取数据