修复
This commit is contained in:
@@ -80,6 +80,7 @@
|
|||||||
placeholder="请选择父部门"
|
placeholder="请选择父部门"
|
||||||
clearable
|
clearable
|
||||||
style="width: 100%"
|
style="width: 100%"
|
||||||
|
@change="handleParentChange"
|
||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="部门名称" prop="name">
|
<el-form-item label="部门名称" prop="name">
|
||||||
@@ -181,7 +182,8 @@ const cascaderProps = {
|
|||||||
value: 'value',
|
value: 'value',
|
||||||
label: 'label',
|
label: 'label',
|
||||||
children: 'children',
|
children: 'children',
|
||||||
checkStrictly: true
|
checkStrictly: true,
|
||||||
|
emitPath: false // 只返回最后选中的节点ID,而不是路径数组
|
||||||
};
|
};
|
||||||
|
|
||||||
// 树形组件配置
|
// 树形组件配置
|
||||||
@@ -322,6 +324,16 @@ function isNodeInTree(node: SysDept, tree: SysDept[]): boolean {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理父部门选择变化
|
||||||
|
function handleParentChange(value: any) {
|
||||||
|
// 如果级联选择器返回的是数组(尽管配置了emitPath: false),取最后一个元素
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
formData.parentID = value[value.length - 1];
|
||||||
|
} else {
|
||||||
|
formData.parentID = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 新增部门
|
// 新增部门
|
||||||
function handleAdd() {
|
function handleAdd() {
|
||||||
isEdit.value = false;
|
isEdit.value = false;
|
||||||
@@ -332,7 +344,10 @@ function handleAdd() {
|
|||||||
// 新增子部门
|
// 新增子部门
|
||||||
function handleAddChild(row: SysDept) {
|
function handleAddChild(row: SysDept) {
|
||||||
isEdit.value = false;
|
isEdit.value = false;
|
||||||
formData.parentID = row.deptID;
|
// 确保 parentID 是字符串而不是数组
|
||||||
|
formData.parentID = Array.isArray(row.deptID)
|
||||||
|
? row.deptID[row.deptID.length - 1]
|
||||||
|
: row.deptID;
|
||||||
dialogVisible.value = true;
|
dialogVisible.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,6 +355,10 @@ function handleAddChild(row: SysDept) {
|
|||||||
function handleEdit(row: SysDept) {
|
function handleEdit(row: SysDept) {
|
||||||
isEdit.value = true;
|
isEdit.value = true;
|
||||||
Object.assign(formData, row);
|
Object.assign(formData, row);
|
||||||
|
// 确保 parentID 是字符串而不是数组
|
||||||
|
if (Array.isArray(formData.parentID)) {
|
||||||
|
formData.parentID = formData.parentID[formData.parentID.length - 1];
|
||||||
|
}
|
||||||
dialogVisible.value = true;
|
dialogVisible.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,11 +394,18 @@ async function handleSubmit() {
|
|||||||
await formRef.value.validate();
|
await formRef.value.validate();
|
||||||
submitting.value = true;
|
submitting.value = true;
|
||||||
|
|
||||||
|
// 防御性处理:确保parentID是字符串而不是数组
|
||||||
|
const submitData = { ...formData };
|
||||||
|
if (Array.isArray(submitData.parentID)) {
|
||||||
|
// 如果是数组,取最后一个元素(叶子节点)
|
||||||
|
submitData.parentID = submitData.parentID[submitData.parentID.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
if (isEdit.value) {
|
if (isEdit.value) {
|
||||||
await deptApi.updateDept(formData);
|
await deptApi.updateDept(submitData);
|
||||||
ElMessage.success('更新成功');
|
ElMessage.success('更新成功');
|
||||||
} else {
|
} else {
|
||||||
await deptApi.createDept(formData);
|
await deptApi.createDept(submitData);
|
||||||
ElMessage.success('新增成功');
|
ElMessage.success('新增成功');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user