From 18a2dc33667c279d82a59ddc68db290bc1c0cd57 Mon Sep 17 00:00:00 2001 From: ziv Date: Fri, 29 Apr 2022 01:46:10 +0000 Subject: [PATCH] =?UTF-8?q?decodeFields=20=20=E5=A2=9E=E5=8A=A0=E8=A1=A8?= =?UTF-8?q?=E5=8D=95=E5=88=9D=E5=A7=8B=E5=8C=96=E8=83=BD=E5=8A=9B,=20?= =?UTF-8?q?=E5=AE=8C=E7=BE=8E=E6=94=AF=E6=8C=81=E5=AD=90=E7=BA=A7=E5=85=83?= =?UTF-8?q?=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-ui-admin/src/utils/formGenerator.js | 27 +++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/yudao-ui-admin/src/utils/formGenerator.js b/yudao-ui-admin/src/utils/formGenerator.js index 248e0b23e..d7417d9b1 100644 --- a/yudao-ui-admin/src/utils/formGenerator.js +++ b/yudao-ui-admin/src/utils/formGenerator.js @@ -1,13 +1,32 @@ /** * 将服务端返回的 fields 字符串数组,解析成 JSON 数组 + * 如果指定了 variables 参数可对表单进行初始化 * * @param fields JSON 字符串数组 + * @param variables Object 表单初始值 * @returns {*[]} JSON 数组 */ -export function decodeFields(fields) { - const drawingList = [] - fields.forEach(item => { - drawingList.push(JSON.parse(item)) +export function decodeFields(fields, variables) { + const drawingList = (fields || []).map(json => { + const item = JSON.parse(json) + + if (typeof variables === 'undefined' ) return item + + const setDefault = (item, variables) => { + if (typeof variables[item.__vModel__] !== 'undefined') { + item.__config__.defaultValue = variables[item.__vModel__] + } + if (item.__config__.children && item.__config__.children.length) { + item.__config__.children.forEach(child => { + setDefault(child, variables) + }) + } + } + + setDefault(item, variables) + + return item }) + return drawingList }