1.封装组件将Json数据转换成级联数据
export function setTree(list, parentId = 0) {
const arr = [];
list.forEach((item) => {
if (item.parentId === parentId) {
const children = setTree(list, item.id);
if (children.length > 0) {
item.children = children;
}
arr.push(item);
}
});
return arr;
}
2.只有最后一级id,反查出父级id
//回显
findId(array, id, arr) {
for (let i = 0; i < array.length; i++) {
if (array[i].id === id) {
arr.unshift(array[i].id);
this.addData.reportingUnit = arr;
return arr;
} else if (Array.isArray(array[i].children)) {
const result = this.findId(array[i].children, id, arr);
if (result) {
arr.unshift(array[i].id);
this.addData.reportingUnit = arr;
return result;
}
}
}
return null;
},
3.
//报备单位下拉列表
getReportingUnit() {
const id = this.$store.getters.userInfo.deptId;
this.$api.affair.getDept({ region: this.$store.getters.userInfo.dept.region })
.then((res) => {
this.array = setTree(res.data, res.data[0].parentId);
this.findId(this.array, id, this.list);
});
},