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:
2026-06-29 21:17:38 +08:00
parent bba08dadcc
commit 3b0887b126
3 changed files with 8 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "shrimp-admin", "name": "shrimp-admin",
"version": "0.3.7", "version": "0.3.8",
"private": true, "private": true,
"description": "Shrimp 框架前端 — 消费 ShrimpServer + ShrimpTable 契约的经典 Vue3 + Element Plus 实现", "description": "Shrimp 框架前端 — 消费 ShrimpServer + ShrimpTable 契约的经典 Vue3 + Element Plus 实现",
"main": "src/index.ts", "main": "src/index.ts",
+4 -2
View File
@@ -334,8 +334,10 @@ async function handleSave() {
formFields.value.forEach(field => { formFields.value.forEach(field => {
if (field.editable === 'readonly') return if (field.editable === 'readonly') return
if (field.editable === 'addOnly') return if (field.editable === 'addOnly') return
const newValue = formData.value[field.key] // 清空字段值为 undefined 时,JSON.stringify 会丢键 → 后端收不到、看似"提交无效"。
const oldValue = originalData.value[field.key] // 归一成 null:既保证清空能提交(后端置空),又让"两边都空"不误判为有改动。
const newValue = formData.value[field.key] ?? null
const oldValue = originalData.value[field.key] ?? null
if (newValue === oldValue) return if (newValue === oldValue) return
submitData[field.key] = newValue submitData[field.key] = newValue
}) })
+3 -2
View File
@@ -39,9 +39,10 @@ const emit = defineEmits<{
'update:modelValue': [value: any] 'update:modelValue': [value: any]
}>() }>()
// 编辑态清空:el-select 单选清空发 '',但 relation 是 ObjectId,存 '' 会 cast 报错 → 归一成 null(取消关联) // 编辑态清空:el-select 单选清空发 '' 或 undefined,relation 是 ObjectId、且 undefined 会被
// JSON.stringify 丢键 → 都归一成 null(取消关联)。多选清空是 [],原样。
function onEdit(v: any) { 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 }[]>([]) const options = ref<{ label: string; value: string }[]>([])