級聯選擇器綁定父級id的方法

1.查詢級聯選擇器部門接口的內容this

queryDeptTree() {spa

var userId = this.$store.getters.accountId遞歸

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

this.deptOptions = response.data.dataget

this.makeDeptMap(this.deptOptions)      //將拿到的部門結構id作成一張mapio

})List

}map

2.製做map查詢

//遞歸存儲到deptMapmake

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]

}

}

相關文章
相關標籤/搜索