From 4e7cf020298cd844f39e9425cca31671fd79c891 Mon Sep 17 00:00:00 2001 From: WUSIJIAN <13825895+wsj0228@user.noreply.gitee.com> Date: Tue, 16 Dec 2025 18:01:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E9=80=89=E9=A1=B9=E6=95=B0=E6=8D=AE=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91,=E6=94=AF=E6=8C=81=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=95=B0=E7=BB=84=E4=B8=8E=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=8F=8C=E5=90=91=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../category/component/editCategory.vue | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/views/assets/category/component/editCategory.vue b/src/views/assets/category/component/editCategory.vue index c3f20dc..bfb06ae 100644 --- a/src/views/assets/category/component/editCategory.vue +++ b/src/views/assets/category/component/editCategory.vue @@ -276,9 +276,10 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => { ruleForm.name = data.name || ''; ruleForm.sort = data.sort || 0; ruleForm.status = data.status || 'enabled'; - // 处理 attrs 中的 options 字段(后端可能返回字符串格式) + // 处理 attrs 中的 options 字段 ruleForm.attrs = (data.attrs || []).map((attr: any) => { let options = attr.options; + // 后端可能返回字符串格式,先解析 if (typeof options === 'string') { try { options = JSON.parse(options); @@ -286,6 +287,11 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => { options = []; } } + // 如果是对象数组 [{label, value}],转换为简单数组 [value] + // 这样前端选择器才能正确工作 + if (Array.isArray(options) && options.length > 0 && typeof options[0] === 'object') { + options = options.map((opt: any) => opt.value || opt.key || opt); + } return { ...attr, options: options || [] }; }); // 如果有单选/多选属性,预加载字典类型数据 @@ -321,15 +327,24 @@ const onSubmit = () => { const processedAttrs = ruleForm.attrs.map((attr) => { if (attr.type === 'select' || attr.type === 'multi_select') { const { name, ...rest } = attr; - // 将 options 从 ["red"] 转换为 [{"label":"红色","value":"red"}] - const dictValues = getDictValuesByType(attr.description || ''); - const formattedOptions = (attr.options || []).map((optValue: string) => { - const dictItem = dictValues.find((d: any) => d.key === optValue); - return { - label: dictItem?.value || optValue, - value: optValue, - }; - }); + const options = attr.options || []; + let formattedOptions: any[] = []; + + // 判断 options 是否已经是对象数组格式 + if (options.length > 0 && typeof options[0] === 'object') { + // 已经是对象数组格式,直接使用 + formattedOptions = options; + } else { + // 是字符串数组,需要转换为 [{label, value}] 格式 + const dictValues = getDictValuesByType(attr.description || ''); + formattedOptions = options.map((optValue: string) => { + const dictItem = dictValues.find((d: any) => d.key === optValue); + return { + label: dictItem?.value || optValue, + value: optValue, + }; + }); + } return { ...rest, options: formattedOptions }; } return attr;