Skip to content

Commit d1dbb20

Browse files
YunaiVgitee-org
authored andcommitted
!143 decodeFields 增加表单初始化能力, 完美支持子级元素
Merge pull request !143 from ziv/master
2 parents 23fd6a7 + 55716ad commit d1dbb20

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/utils/formGenerator.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
/**
22
* 将服务端返回的 fields 字符串数组,解析成 JSON 数组
3+
* 如果指定了 variables 参数可对表单进行初始化
34
*
45
* @param fields JSON 字符串数组
6+
* @param variables Object 表单初始值
57
* @returns {*[]} JSON 数组
68
*/
7-
export function decodeFields(fields) {
8-
const drawingList = []
9-
fields.forEach(item => {
10-
drawingList.push(JSON.parse(item))
9+
export function decodeFields(fields, variables) {
10+
const drawingList = (fields || []).map(json => {
11+
const item = JSON.parse(json)
12+
13+
if (typeof variables === 'undefined' ) return item
14+
15+
const setDefault = (item, variables) => {
16+
if (typeof variables[item.__vModel__] !== 'undefined') {
17+
item.__config__.defaultValue = variables[item.__vModel__]
18+
}
19+
if (item.__config__.children && item.__config__.children.length) {
20+
item.__config__.children.forEach(child => {
21+
setDefault(child, variables)
22+
})
23+
}
24+
}
25+
26+
setDefault(item, variables)
27+
28+
return item
1129
})
30+
1231
return drawingList
1332
}

0 commit comments

Comments
 (0)