级联选择器绑定父级id的方法

1.查询级联选择器部门接口的内容

queryDeptTree() {

var userId = this.$store.getters.accountId

enterpriseDeptTree({ 'user_id': userId }).then(response => {

this.deptOptions = response.data.data

this.makeDeptMap(this.deptOptions)      //将拿到的部门结构id做成一张map

})

}

2.制作map

//递归存储到deptMap

makeDeptMap(currentDept) {

var dept

if (currentDept.length === 1) {

dept = currentDept[0]

this.deptMap[dept.deptId + ''] = dept.pid          //pid为父节点id

if (dept.childEnterpriseDeptList !== undefined && dept.childEnterpriseDeptList.length > 0) {

this.makeDeptMap(dept.childEnterpriseDeptList)

}

} else {

currentDept.forEach(curDept => {

dept = curDept

this.deptMap[dept.deptId + ''] = dept.pid

if (dept.childEnterpriseDeptList !== undefined && dept.childEnterpriseDeptList.length > 0) {

this.makeDeptMap(dept.childEnterpriseDeptList)

}

})

}

}

3.找当前id的父id制作id串:

this.findAncestorById(this.oldDeptId)

findAncestorById(deptId) {

this.contentDept.unshift(deptId)

var curDeptId = this.deptMap[deptId]

while (curDeptId !== undefined && curDeptId !== 0) {

this.contentDept.unshift(curDeptId)

curDeptId = this.deptMap[curDeptId]

}

}

猜你喜欢

转载自my.oschina.net/u/3762697/blog/1633965