具体的数据还请各位根据自己项目而定
实现效果:
1.多选为一级时回显:
2.多选为二级时回显:
代码:
// 回显服务类别
showInsured () {
console.log(this.form.serviceType) // 这个是点击修改时获取到的多选值
let tempInsured = this.form.serviceType ? this.form.serviceType.split(',') : []
let insured = []
for (let i = 0;i < tempInsured.length;i++) {
let arr = [tempInsured[i]]
console.log(tempInsured[i], 'ppp')
let temp = this.findParent(tempInsured[i])
if (temp != 0) {
arr.unshift(this.findParent(tempInsured[i]))
}
console.log(arr, '000')
insured.push(arr)
}
this.form.serviceType = insured
console.log(this.form.serviceType)
},
// 查找父节点
findParent (childeNode) {
console.log(childeNode)
console.log(this.productTypeList) // 当前下拉数组
let parentNode = 0
for (let i = 0;i < this.productTypeList.length;i++) {
if (this.productTypeList[i].children) {
if (this.productTypeList[i].value == childeNode) {
return 0 // 说明当前节点已为父节点
} else {
console.log(this.productTypeList[i])
let index = this.productTypeList[i].children.findIndex(item => item.value == childeNode)
console.log(index)
if (index != -1) {
parentNode = this.productTypeList[i].value
break
}
}
}
}
return parentNode
},