Commit 56c18dcf authored by xinzhedeai's avatar xinzhedeai

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

parent ebf3942b
......@@ -144,14 +144,12 @@
</el-col>
<el-col :span="12">
<el-form-item label="所在部门" prop="deptId">
<el-cascader
v-model="form.deptPath"
<treeselect
v-model="form.deptIds"
:options="deptOptions"
:props="cascaderProps"
placeholder="请选择所在部门"
clearable
@change="handleDeptChange"
></el-cascader>
placeholder="请选择部门"
:normalizer="normalizer"
></treeselect>
</el-form-item>
</el-col>
<el-col :span="12">
......@@ -298,8 +296,12 @@ import {
import { getDict } from "@/api/jinrun/common";
import { getDept, getPosition } from "@/api/jinrun/zhongkong";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
name: "Info",
components: { Treeselect },
data() {
return {
// 级联选择器配置
......@@ -395,94 +397,26 @@ export default {
},
methods: {
/** 递归处理部门树,转换为级联选择器需要的格式 */
processDepartmentTreeForCascader(departments) {
// 直接返回处理后的部门树,不需要扁平化
return departments.map((dept) => {
const processedDept = {
index: dept.index,
name: dept.name,
children: [],
normalizer(node) {
return {
id: node.index, // 用index作为唯一标识
label: node.name, // 用name作为显示文本
children: node.children || [], // 子节点(如果为null则转为空数组)
};
// 如果有子部门,递归处理
if (dept.children && dept.children.length > 0) {
processedDept.children = this.processDepartmentTreeForCascader(
dept.children
);
} else {
// 如果没有子部门,设置为null
processedDept.children = null;
}
return processedDept;
});
},
/** 获取部门列表 */
getDeptList() {
getDept({})
.then((response) => {
console.log(response.data, "部门数据");
// 检查数据格式是否正确
if (
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("部门数据格式不正确");
}
this.deptOptions = response.data.data;
})
.catch((error) => {
console.error("获取部门列表失败:", error);
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() {
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