!143 decodeFields 增加表单初始化能力, 完美支持子级元素

Merge pull request !143 from ziv/master
This commit is contained in:
芋道源码 2022-08-19 12:20:20 +00:00 committed by Gitee
commit b5d458f1c4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 23 additions and 4 deletions

View File

@ -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
}