需求是通过el-tree做一份组织架构的更换部门
需要获得最底层的部门的id,和从上累计父部门的id
在初始化拿数据的时候
先递归遍历判断是否是下面有没有子部门
有的话累计id,同时记录父部门下有几个子元素,要重复赋值
赋值到最后一个子元素后要清空父元素,同时传入最上级的id,因为已经被清空了,所有最先记录了
let father = [] // 暂存父元素id
let indexNow = 0 // 记录当前子元素第几个
let indexNum = 0 // 记录父元素下有几个子元素
let companyId = '' // 记录总公司id
let index = 0 // 判断是不是第一个总公司
function recursive(departmentMenuList) {
if (index === 0) {
companyId = departmentMenuList[0].departmentId
}
index++
for (var i = 0; i < departmentMenuList.length; i++) {
if (departmentMenuList[i].childList) {
indexNum = departmentMenuList[i].childList.length
father.push(departmentMenuList[i].departmentId)
departmentMenuList[i].departmentId = null
recursive(departmentMenuList[i].childList)
} else {
departmentMenuList[i].father = father
indexNow++
if (indexNum === indexNow) {
father = []
father.push(companyId)
indexNow = 0
indexNum = 0
}
}
}
}