Commit b5d458f1c49e3bede6e1d401fb5fcf08456bfcc5

Authored by 芋道源码
Committed by Gitee
2 parents 95e6b390 0ca750a8

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

Merge pull request !143 from ziv/master
yudao-ui-admin/src/utils/formGenerator.js
1 1 /**
2 2 * 将服务端返回的 fields 字符串数组,解析成 JSON 数组
  3 + * 如果指定了 variables 参数可对表单进行初始化
3 4 *
4 5 * @param fields JSON 字符串数组
  6 + * @param variables Object 表单初始值
5 7 * @returns {*[]} JSON 数组
6 8 */
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
11 29 })
  30 +
12 31 return drawingList
13 32 }
... ...