Commit 56c18dcf authored by xinzhedeai's avatar xinzhedeai

add:所属部门树状数据下拉

parent ebf3942b
...@@ -144,14 +144,12 @@ ...@@ -144,14 +144,12 @@
</el-col> </el-col>
<el-col :span="12"> <el-col :span="12">
<el-form-item label="所在部门" prop="deptId"> <el-form-item label="所在部门" prop="deptId">
<el-cascader <treeselect
v-model="form.deptPath" v-model="form.deptIds"
:options="deptOptions" :options="deptOptions"
:props="cascaderProps" placeholder="请选择部门"
placeholder="请选择所在部门" :normalizer="normalizer"
clearable ></treeselect>
@change="handleDeptChange"
></el-cascader>
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :span="12"> <el-col :span="12">
...@@ -298,8 +296,12 @@ import { ...@@ -298,8 +296,12 @@ import {
import { getDict } from "@/api/jinrun/common"; import { getDict } from "@/api/jinrun/common";
import { getDept, getPosition } from "@/api/jinrun/zhongkong"; import { getDept, getPosition } from "@/api/jinrun/zhongkong";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default { export default {
name: "Info", name: "Info",
components: { Treeselect },
data() { data() {
return { return {
// 级联选择器配置 // 级联选择器配置
...@@ -395,94 +397,26 @@ export default { ...@@ -395,94 +397,26 @@ export default {
}, },
methods: { methods: {
/** 递归处理部门树,转换为级联选择器需要的格式 */ normalizer(node) {
processDepartmentTreeForCascader(departments) { return {
// 直接返回处理后的部门树,不需要扁平化 id: node.index, // 用index作为唯一标识
return departments.map((dept) => { label: node.name, // 用name作为显示文本
const processedDept = { children: node.children || [], // 子节点(如果为null则转为空数组)
index: dept.index,
name: dept.name,
children: [],
}; };
// 如果有子部门,递归处理
if (dept.children && dept.children.length > 0) {
processedDept.children = this.processDepartmentTreeForCascader(
dept.children
);
} else {
// 如果没有子部门,设置为null
processedDept.children = null;
}
return processedDept;
});
}, },
/** 获取部门列表 */ /** 获取部门列表 */
getDeptList() { getDeptList() {
getDept({}) getDept({})
.then((response) => { .then((response) => {
console.log(response.data, "部门数据"); console.log(response.data, "部门数据");
// 检查数据格式是否正确 // 检查数据格式是否正确
if ( this.deptOptions = response.data.data;
response.data &&
Array.isArray(response.data) &&
response.data.length > 0
) {
// 获取根节点的子部门并处理为级联格式
const rootChildren = response.data[0].children || [];
this.deptOptions =
this.processDepartmentTreeForCascader(rootChildren);
} else {
this.deptOptions = [];
console.error("部门数据格式不正确");
}
}) })
.catch((error) => { .catch((error) => {
console.error("获取部门列表失败:", error); console.error("获取部门列表失败:", error);
this.deptOptions = []; this.deptOptions = [];
}); });
}, },
/** 处理部门选择变化 */
handleDeptChange(value) {
if (value && value.length > 0) {
// 保存最后一级的部门ID作为deptId
this.form.deptId = value[value.length - 1];
// 查找选择的部门对象,获取部门名称
if (this.deptOptions && this.deptOptions.length > 0) {
let selectedDept = this.findDeptById(
this.deptOptions,
this.form.deptId
);
if (selectedDept) {
this.form.deptName = selectedDept.name;
}
}
} else {
this.form.deptId = null;
this.form.deptName = null;
}
},
/** 根据ID查找部门对象 */
findDeptById(departments, targetId) {
for (const dept of departments) {
if (dept.index === targetId) {
return dept;
}
if (dept.children && dept.children.length > 0) {
const found = this.findDeptById(dept.children, targetId);
if (found) {
return found;
}
}
}
return null;
},
/** 获取岗位列表 */ /** 获取岗位列表 */
getPositionList() { getPositionList() {
getPosition({ getPosition({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment