1. 对象里面添加数组对象

111.png

  • 发送post提交的接口参数

    1. {
    2. "f5768c21": [
    3. "subf642c26"
    4. ],
    5. "f5768c102": [
    6. "subf761c10",
    7. "subf761c13"
    8. ]
    9. }
  • 接口JSON数据返回

    1. [
    2. {
    3. "atrName": "subf642c26",
    4. "columnName": "subf642c26",
    5. "formId": 5768,
    6. "dataTypeId": 1,
    7. "showName": "子表单第一个-单行文本",
    8. "parentType": "subform",
    9. "parentColumnName": "f5768c21",
    10. "type": "input"
    11. },
    12. {
    13. "atrName": "subf761c10",
    14. "columnName": "subf761c10",
    15. "formId": 5768,
    16. "dataTypeId": 1,
    17. "showName": "子表单第二个-单行文本",
    18. "parentType": "subform",
    19. "parentColumnName": "f5768c102",
    20. "type": "input"
    21. },
    22. {
    23. "atrName": "subf761c13",
    24. "columnName": "subf761c13",
    25. "formId": 5768,
    26. "dataTypeId": 3,
    27. "showName": "子表单第二个-数字",
    28. "parentType": "subform",
    29. "parentColumnName": "f5768c102",
    30. "type": "countNumber"
    31. },
    32. {
    33. "formId": 5768,
    34. "atrName": "username",
    35. "itemType": 2,
    36. "showName": "提交人",
    37. "formName": "综合组件",
    38. "dataTypeId": 1,
    39. "options": null,
    40. "id": "5768_5",
    41. "type": "submitPerson",
    42. "columnName": "user_table_f5768.username"
    43. },
    44. {
    45. "formId": 5768,
    46. "atrName": "createTimeF5768",
    47. "itemType": 2,
    48. "showName": "提交时间",
    49. "formName": "综合组件",
    50. "dataTypeId": 4,
    51. "options": null,
    52. "id": "5768_6",
    53. "type": "submitTime",
    54. "columnName": "create_time_f5768"
    55. },
    56. {
    57. "formId": 5768,
    58. "atrName": "updateTimeF5768",
    59. "itemType": 2,
    60. "showName": "更新时间",
    61. "formName": "综合组件",
    62. "dataTypeId": 4,
    63. "options": null,
    64. "id": "5768_7",
    65. "type": "UpdateTime",
    66. "columnName": "update_time_f5768"
    67. }
    68. ]
  • 代码

  • 首先判断是否有父ld值,没有先初始化存进去为空数组,之后再空数组push添加新内容完成赋值

    1. const itemResultList = {}
    2. for (let i = 0; i < this.select.options.field.length; i++) {
    3. if (this.select.options.field[i].parentType === 'subform') {
    4. // 判断父类是否存在
    5. if (itemResultList[this.select.options.field[i].parentColumnName]) {
    6. itemResultList[this.select.options.field[i].parentColumnName].push(this.select.options.field[i].columnName)
    7. } else {
    8. itemResultList[this.select.options.field[i].parentColumnName] = []
    9. itemResultList[this.select.options.field[i].parentColumnName].push(this.select.options.field[i].columnName)
    10. }
    11. } else {
    12. array.push(this.select.options.field[i].atrName)
    13. }
    14. }
  • 另外一种写法

    1. for (const item of this.select.options.field) {
    2. if (item.parentType && item.parentType === 'subform') {
    3. const parentColumnName = item.parentColumnName
    4. const indexA = dataList.findIndex(x => x.formId === item.formId)
    5. if (dataList[indexA].itemResultList[parentColumnName]) {
    6. dataList[indexA].itemResultList[parentColumnName].push(item.atrName)
    7. } else {
    8. dataList[indexA].itemResultList[parentColumnName] = []
    9. dataList[indexA].itemResultList[parentColumnName].push(item.atrName)
    10. }
    11. } else {
    12. // 查询那个表
    13. const indexA = dataList.findIndex(x => x.formId === item.formId)
    14. // 索引存在且索引下的包含这个表单id名
    15. if (indexA !== -1 && !dataList[indexA].resultList.includes(item.atrName)) {
    16. dataList[indexA].resultList.push(item.atrName)
    17. }
    18. }
    19. }

2.普通字段和子表单数据分类处理

  • 原始接口数据
    1. [
    2. {
    3. "formId": 5768,
    4. "itemType": 2,
    5. "showName": "普通数字",
    6. "subformUuid": null,
    7. "colWidth": null,
    8. "dataTypeId": 3,
    9. "type": "countNumber",
    10. "atrName": "f5768c116",
    11. "formName": "综合组件",
    12. "options": {
    13. "mustFill": false,
    14. "readOnly": false,
    15. "hide": false,
    16. "defaultValueShow": false,
    17. "int": false,
    18. "rangeIndex": false,
    19. "min": 0,
    20. "max": 0,
    21. "setFloat": 2,
    22. "controlsPosition": "right",
    23. "describe": "",
    24. "dataLinkFlag": false,
    25. "dataLinkage": {
    26. "dialogFlag": false,
    27. "currentFormList": [],
    28. "currentFormItem": "",
    29. "otherFormId": "",
    30. "otherFormList": [],
    31. "otherFormListShow": [],
    32. "otherFormCurrentItem": "",
    33. "otherFormDataLinkageItem": ""
    34. },
    35. "remoteFunc": "func_1638870650000_2482"
    36. },
    37. "id": "5768_2",
    38. "key": "1638870650000_2482",
    39. "columnName": "f5768c116"
    40. },
    41. {
    42. "atrName": "subf761c13",
    43. "columnName": "subf761c13",
    44. "formId": 5768,
    45. "dataTypeId": 3,
    46. "options": {
    47. "mustFill": false,
    48. "readOnly": false,
    49. "hide": false,
    50. "defaultValueShow": false,
    51. "int": false,
    52. "rangeIndex": false,
    53. "min": 0,
    54. "max": 0,
    55. "setFloat": 2,
    56. "controlsPosition": "right",
    57. "describe": "",
    58. "dataLinkFlag": false,
    59. "dataLinkage": {
    60. "dialogFlag": false,
    61. "currentFormList": [],
    62. "currentFormItem": "",
    63. "otherFormId": "",
    64. "otherFormList": [],
    65. "otherFormListShow": [],
    66. "otherFormCurrentItem": "",
    67. "otherFormDataLinkageItem": ""
    68. },
    69. "remoteFunc": "func_1638522586000_18918"
    70. },
    71. "showName": "子表单第二个-数字",
    72. "parentName": "子表单第二个",
    73. "parentType": "subform",
    74. "parentColumnName": "f5768c102",
    75. "type": "countNumber"
    76. },
    77. {
    78. "atrName": "subf642c26",
    79. "columnName": "subf642c26",
    80. "formId": 5768,
    81. "dataTypeId": 1,
    82. "options": {
    83. "width": "100",
    84. "format": "text",
    85. "dataType": "string",
    86. "describe": "",
    87. "scan": false,
    88. "mustFill": false,
    89. "readOnly": false,
    90. "hide": false,
    91. "NotAllowedRepeat": false,
    92. "placeholder": "",
    93. "defaultValue": "",
    94. "previewValue": "",
    95. "maxlength": "",
    96. "placeholderShow": false,
    97. "defaultValueShow": false,
    98. "maxLengthShow": false,
    99. "dataLinkFlag": false,
    100. "dataLinkage": {
    101. "dialogFlag": false,
    102. "currentFormList": [],
    103. "currentFormItem": "",
    104. "otherFormId": "",
    105. "otherFormList": [],
    106. "otherFormListShow": [],
    107. "otherFormCurrentItem": "",
    108. "otherFormDataLinkageItem": ""
    109. },
    110. "remoteFunc": "func_1637143238000_70054"
    111. },
    112. "showName": "子表单第一个-单行文本",
    113. "parentName": "子表单第一个",
    114. "parentType": "subform",
    115. "parentColumnName": "f5768c21",
    116. "type": "input"
    117. },
    118. {
    119. "atrName": "subf642c27",
    120. "columnName": "subf642c27",
    121. "formId": 5768,
    122. "dataTypeId": 2,
    123. "options": {
    124. "width": "100",
    125. "defaultValue": "",
    126. "previewValue": "",
    127. "pattern": "",
    128. "placeholder": "",
    129. "describe": "",
    130. "maxlength": "",
    131. "mustFill": false,
    132. "readOnly": false,
    133. "hide": false,
    134. "placeholderShow": false,
    135. "defaultValueShow": false,
    136. "maxLengthShow": false,
    137. "dataLinkFlag": false,
    138. "dataLinkage": {
    139. "dialogFlag": false,
    140. "currentFormList": [],
    141. "currentFormItem": "",
    142. "otherFormId": "",
    143. "otherFormList": [],
    144. "otherFormListShow": [],
    145. "otherFormCurrentItem": "",
    146. "otherFormDataLinkageItem": ""
    147. },
    148. "remoteFunc": "func_1638754212000_56027"
    149. },
    150. "showName": "子表单第一个-多行文本",
    151. "parentName": "子表单第一个",
    152. "parentType": "subform",
    153. "parentColumnName": "f5768c21",
    154. "type": "textarea"
    155. },
    156. {
    157. "atrName": "subf761c10",
    158. "columnName": "subf761c10",
    159. "formId": 5768,
    160. "dataTypeId": 1,
    161. "options": {
    162. "width": "100",
    163. "format": "text",
    164. "dataType": "string",
    165. "describe": "",
    166. "scan": false,
    167. "mustFill": false,
    168. "readOnly": false,
    169. "hide": false,
    170. "NotAllowedRepeat": false,
    171. "placeholder": "",
    172. "defaultValue": "",
    173. "previewValue": "",
    174. "maxlength": "",
    175. "placeholderShow": false,
    176. "defaultValueShow": false,
    177. "maxLengthShow": false,
    178. "dataLinkFlag": false,
    179. "dataLinkage": {
    180. "dialogFlag": false,
    181. "currentFormList": [],
    182. "currentFormItem": "",
    183. "otherFormId": "",
    184. "otherFormList": [],
    185. "otherFormListShow": [],
    186. "otherFormCurrentItem": "",
    187. "otherFormDataLinkageItem": ""
    188. },
    189. "remoteFunc": "func_1638522502000_72596"
    190. },
    191. "showName": "子表单第二个-单行文本",
    192. "parentName": "子表单第二个",
    193. "parentType": "subform",
    194. "parentColumnName": "f5768c102",
    195. "type": "input"
    196. }
    197. ]
  • 修改后的接口数据

    1. [
    2. {
    3. "formId": 5768,
    4. "itemType": 2,
    5. "showName": "普通数字",
    6. "subformUuid": null,
    7. "colWidth": null,
    8. "dataTypeId": 3,
    9. "type": "countNumber",
    10. "atrName": "f5768c116",
    11. "formName": "综合组件",
    12. "options": {
    13. "mustFill": false,
    14. "readOnly": false,
    15. "hide": false,
    16. "defaultValueShow": false,
    17. "int": false,
    18. "rangeIndex": false,
    19. "min": 0,
    20. "max": 0,
    21. "setFloat": 2,
    22. "controlsPosition": "right",
    23. "describe": "",
    24. "dataLinkFlag": false,
    25. "dataLinkage": {
    26. "dialogFlag": false,
    27. "currentFormList": [],
    28. "currentFormItem": "",
    29. "otherFormId": "",
    30. "otherFormList": [],
    31. "otherFormListShow": [],
    32. "otherFormCurrentItem": "",
    33. "otherFormDataLinkageItem": ""
    34. },
    35. "remoteFunc": "func_1638870650000_2482"
    36. },
    37. "id": "5768_2",
    38. "key": "1638870650000_2482",
    39. "columnName": "f5768c116"
    40. },
    41. {
    42. "parentColumnName": "f5768c102",
    43. "children": [
    44. {
    45. "atrName": "subf761c13",
    46. "columnName": "subf761c13",
    47. "formId": 5768,
    48. "dataTypeId": 3,
    49. "options": {
    50. "mustFill": false,
    51. "readOnly": false,
    52. "hide": false,
    53. "defaultValueShow": false,
    54. "int": false,
    55. "rangeIndex": false,
    56. "min": 0,
    57. "max": 0,
    58. "setFloat": 2,
    59. "controlsPosition": "right",
    60. "describe": "",
    61. "dataLinkFlag": false,
    62. "dataLinkage": {
    63. "dialogFlag": false,
    64. "currentFormList": [],
    65. "currentFormItem": "",
    66. "otherFormId": "",
    67. "otherFormList": [],
    68. "otherFormListShow": [],
    69. "otherFormCurrentItem": "",
    70. "otherFormDataLinkageItem": ""
    71. },
    72. "remoteFunc": "func_1638522586000_18918"
    73. },
    74. "showName": "子表单第二个-数字",
    75. "parentName": "子表单第二个",
    76. "parentType": "subform",
    77. "parentColumnName": "f5768c102",
    78. "type": "countNumber"
    79. },
    80. {
    81. "atrName": "subf761c10",
    82. "columnName": "subf761c10",
    83. "formId": 5768,
    84. "dataTypeId": 1,
    85. "options": {
    86. "width": "100",
    87. "format": "text",
    88. "dataType": "string",
    89. "describe": "",
    90. "scan": false,
    91. "mustFill": false,
    92. "readOnly": false,
    93. "hide": false,
    94. "NotAllowedRepeat": false,
    95. "placeholder": "",
    96. "defaultValue": "",
    97. "previewValue": "",
    98. "maxlength": "",
    99. "placeholderShow": false,
    100. "defaultValueShow": false,
    101. "maxLengthShow": false,
    102. "dataLinkFlag": false,
    103. "dataLinkage": {
    104. "dialogFlag": false,
    105. "currentFormList": [],
    106. "currentFormItem": "",
    107. "otherFormId": "",
    108. "otherFormList": [],
    109. "otherFormListShow": [],
    110. "otherFormCurrentItem": "",
    111. "otherFormDataLinkageItem": ""
    112. },
    113. "remoteFunc": "func_1638522502000_72596"
    114. },
    115. "showName": "子表单第二个-单行文本",
    116. "parentName": "子表单第二个",
    117. "parentType": "subform",
    118. "parentColumnName": "f5768c102",
    119. "type": "input"
    120. }
    121. ]
    122. },
    123. {
    124. "parentColumnName": "f5768c21",
    125. "children": [
    126. {
    127. "atrName": "subf642c26",
    128. "columnName": "subf642c26",
    129. "formId": 5768,
    130. "dataTypeId": 1,
    131. "options": {
    132. "width": "100",
    133. "format": "text",
    134. "dataType": "string",
    135. "describe": "",
    136. "scan": false,
    137. "mustFill": false,
    138. "readOnly": false,
    139. "hide": false,
    140. "NotAllowedRepeat": false,
    141. "placeholder": "",
    142. "defaultValue": "",
    143. "previewValue": "",
    144. "maxlength": "",
    145. "placeholderShow": false,
    146. "defaultValueShow": false,
    147. "maxLengthShow": false,
    148. "dataLinkFlag": false,
    149. "dataLinkage": {
    150. "dialogFlag": false,
    151. "currentFormList": [],
    152. "currentFormItem": "",
    153. "otherFormId": "",
    154. "otherFormList": [],
    155. "otherFormListShow": [],
    156. "otherFormCurrentItem": "",
    157. "otherFormDataLinkageItem": ""
    158. },
    159. "remoteFunc": "func_1637143238000_70054"
    160. },
    161. "showName": "子表单第一个-单行文本",
    162. "parentName": "子表单第一个",
    163. "parentType": "subform",
    164. "parentColumnName": "f5768c21",
    165. "type": "input"
    166. },
    167. {
    168. "atrName": "subf642c27",
    169. "columnName": "subf642c27",
    170. "formId": 5768,
    171. "dataTypeId": 2,
    172. "options": {
    173. "width": "100",
    174. "defaultValue": "",
    175. "previewValue": "",
    176. "pattern": "",
    177. "placeholder": "",
    178. "describe": "",
    179. "maxlength": "",
    180. "mustFill": false,
    181. "readOnly": false,
    182. "hide": false,
    183. "placeholderShow": false,
    184. "defaultValueShow": false,
    185. "maxLengthShow": false,
    186. "dataLinkFlag": false,
    187. "dataLinkage": {
    188. "dialogFlag": false,
    189. "currentFormList": [],
    190. "currentFormItem": "",
    191. "otherFormId": "",
    192. "otherFormList": [],
    193. "otherFormListShow": [],
    194. "otherFormCurrentItem": "",
    195. "otherFormDataLinkageItem": ""
    196. },
    197. "remoteFunc": "func_1638754212000_56027"
    198. },
    199. "showName": "子表单第一个-多行文本",
    200. "parentName": "子表单第一个",
    201. "parentType": "subform",
    202. "parentColumnName": "f5768c21",
    203. "type": "textarea"
    204. }
    205. ]
    206. }
    207. ]
  • 代码实现

  1. const subData = []
  2. options.field.forEach((item, index) => {
  3. // 判断父类子表单id存在吗?
  4. if (item.parentColumnName && item.parentType === 'subform') {
  5. // 判断是否存在,存在则返回数组对象
  6. const arr = subData.filter(item2 => {
  7. return item2.parentColumnName && item2.parentColumnName === item.parentColumnName
  8. })
  9. // 若空数组先初始化
  10. if (arr.length === 0) {
  11. const obj = {
  12. parentType: item.parentType,
  13. parentName: item.parentName,
  14. parentColumnName: item.parentColumnName,
  15. children: []
  16. }
  17. obj.children.push(item)
  18. subData.push(obj)
  19. } else {
  20. arr[0].children.push(item)
  21. }
  22. } else {
  23. // 普通添加
  24. subData.push(item)
  25. }
  26. this.comOptionField = subData
  27. })