fix(edit): 清空字段能真正提交(undefined→null,避免 JSON 丢键)
清空 relation/select 时 el-* 常 emit undefined,进 submitData 后 JSON.stringify 丢键 → 后端收不到、看似'提交无效'。修: - handleSave 编辑 diff 把值归一成 null(??null),清空 → null → 提交 → 后端置空 - FieldRelation 清空归一成 null(单)/[](多),避免 ObjectId 存 '' cast 报错
This commit is contained in:
@@ -334,8 +334,10 @@ async function handleSave() {
|
||||
formFields.value.forEach(field => {
|
||||
if (field.editable === 'readonly') return
|
||||
if (field.editable === 'addOnly') return
|
||||
const newValue = formData.value[field.key]
|
||||
const oldValue = originalData.value[field.key]
|
||||
// 清空字段值为 undefined 时,JSON.stringify 会丢键 → 后端收不到、看似"提交无效"。
|
||||
// 归一成 null:既保证清空能提交(后端置空),又让"两边都空"不误判为有改动。
|
||||
const newValue = formData.value[field.key] ?? null
|
||||
const oldValue = originalData.value[field.key] ?? null
|
||||
if (newValue === oldValue) return
|
||||
submitData[field.key] = newValue
|
||||
})
|
||||
|
||||
@@ -39,9 +39,10 @@ const emit = defineEmits<{
|
||||
'update:modelValue': [value: any]
|
||||
}>()
|
||||
|
||||
// 编辑态清空:el-select 单选清空发 '',但 relation 是 ObjectId,存 '' 会 cast 报错 → 归一成 null(取消关联)
|
||||
// 编辑态清空:el-select 单选清空发 '' 或 undefined,relation 是 ObjectId、且 undefined 会被
|
||||
// JSON.stringify 丢键 → 都归一成 null(取消关联)。多选清空是 [],原样。
|
||||
function onEdit(v: any) {
|
||||
emit('update:modelValue', v === '' ? null : v)
|
||||
emit('update:modelValue', v === '' || v == null ? (props.field.multiple ? [] : null) : v)
|
||||
}
|
||||
|
||||
const options = ref<{ label: string; value: string }[]>([])
|
||||
|
||||
Reference in New Issue
Block a user