formGenerator.js 900 B

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * 将服务端返回的 fields 字符串数组,解析成 JSON 数组
  3. * 如果指定了 variables 参数可对表单进行初始化
  4. *
  5. * @param fields JSON 字符串数组
  6. * @param variables Object 表单初始值
  7. * @returns {*[]} JSON 数组
  8. */
  9. export function decodeFields(fields, variables) {
  10. const drawingList = (fields || []).map(json => {
  11. const item = JSON.parse(json)
  12. if (typeof variables === 'undefined' ) return item
  13. const setDefault = (item, variables) => {
  14. if (typeof variables[item.__vModel__] !== 'undefined') {
  15. item.__config__.defaultValue = variables[item.__vModel__]
  16. }
  17. if (item.__config__.children && item.__config__.children.length) {
  18. item.__config__.children.forEach(child => {
  19. setDefault(child, variables)
  20. })
  21. }
  22. }
  23. setDefault(item, variables)
  24. return item
  25. })
  26. return drawingList
  27. }