Переглянути джерело

处理子表中使用大量的API传参时的bug问题 对子表调用方法优化代码

wu 3 місяців тому
батько
коміт
c0668a0daf
1 змінених файлів з 57 додано та 31 видалено
  1. 57 31
      src/components/Generator/components/InputTable/index.vue

+ 57 - 31
src/components/Generator/components/InputTable/index.vue

@@ -279,8 +279,11 @@ export default {
         this.oldBatchEditJsonData = deepClone(jsonData)
         if (jsonData && jsonData.length) {
           this.tableFormData = []
-          jsonData.forEach((t) => this.addRow(t))
+          jsonData.forEach((t) => this.addRow(t,false,false,null,true))
           this.tableFormData = this.addRowList.concat(this.tableFormData)
+          for (let i = 0; i < this.tableFormData.length; i++) {
+            this.onChangeFormGetApi(i,null,false)
+          }
         } else {
           this.tableFormData = []
           this.addRow()
@@ -370,37 +373,20 @@ export default {
       const input = child && child.querySelector('input')
       input && input.focus()
     },
-    onFormDataChange:debounce(function(flag,rowIndex, colIndex, tag, scope,editVmodel) {
-      if (this.isAddRow) return
-      this.$emit('change')
-      const data = this.tableFormData[rowIndex][colIndex]
-      //当为下拉框change时
-      //查询当前change的组件是否为表单公共变量
-      if (data.allVar) {
-        sessionStorage.setItem(`FORM_${rowIndex + '_' + data.__vModel__}`, data.value)
-        //在当前行操作
-        this.tableFormData[rowIndex].forEach((i, k) => {
-          if (i.ApiParams && i.ApiParams.length) {
-            if (i.ApiParams.find((o) => o.varType[1] == data.__vModel__)) {
-              getTabelAPIData(i.options, i.propsUrl, i.ApiParams, this.tableFormData[rowIndex], '', i.apiProps).then((res) => {
-                this.$set(i, 'options', res)
-              })
+    // 处理比较复杂的 API事件 API变量等 传参赋值 存储sessionStorage 自定义表达式
+    onChangeFormGetApi(rowIndex,editVmodel,flag){
+      this.tableFormData[rowIndex].forEach((i, k) => {
+        //查询当前组件是否有api变量
+        if (i.tag === 'el-select' && i.ApiReturn && i.ApiReturn.length) {
+          // 获取选中的本条数据所有信息
+          let selectRow = i.options.find((o) => o[i.apiProps.value] === i.value)
+          if (selectRow) {
+            for (const cur of i.ApiReturn) {
+              // 储存全局变量
+              sessionStorage.setItem(`API_${rowIndex + '_' + cur.varName}`, selectRow[cur.keyName])
             }
           }
-        })
-      }
-      //当前组件值change时 查询当前组件是否有api变量
-      if (tag === 'el-select' && data.ApiReturn && data.ApiReturn.length) {
-        // 获取选中的本条数据所有信息
-        let selectRow = data.options.find((o) => o[data.apiProps.value] === data.value)
-        if (selectRow) {
-          for (const cur of data.ApiReturn) {
-            // 储存全局变量
-            sessionStorage.setItem(`API_${rowIndex + '_' + cur.varName}`, selectRow[cur.keyName])
-          }
         }
-      }
-      this.tableFormData[rowIndex].forEach((i, k) => {
         // 查询默认值是否绑定全局变量 并获取 新增时不需要更新  当前change的组件不需要更新
         if (i.defaultVarType && i.defaultVarType.length && editVmodel !== i.__vModel__) {
           const getSessionValue = (prefix) => {
@@ -431,6 +417,38 @@ export default {
           this.getDefaultValueExp.call(this,i,rowIndex)
         }
       })
+    },
+    onFormDataChange:debounce(function(flag,rowIndex, colIndex, tag, scope,editVmodel) {
+      if (this.isAddRow) return
+      this.$emit('change')
+      const data = this.tableFormData[rowIndex][colIndex]
+      //当为下拉框change时
+      //查询当前change的组件是否为表单公共变量
+      if (data.allVar) {
+        sessionStorage.setItem(`FORM_${rowIndex + '_' + data.__vModel__}`, data.value)
+        //在当前行操作
+        this.tableFormData[rowIndex].forEach((i, k) => {
+          if (i.ApiParams && i.ApiParams.length) {
+            if (i.ApiParams.find((o) => o.varType[1] == data.__vModel__)) {
+              getTabelAPIData(i.options, i.propsUrl, i.ApiParams, this.tableFormData[rowIndex], '', i.apiProps).then((res) => {
+                this.$set(i, 'options', res)
+              })
+            }
+          }
+        })
+      }
+      //当前组件值change时 查询当前组件是否有api变量
+      if (tag === 'el-select' && data.ApiReturn && data.ApiReturn.length) {
+        // 获取选中的本条数据所有信息
+        let selectRow = data.options.find((o) => o[data.apiProps.value] === data.value)
+        if (selectRow) {
+          for (const cur of data.ApiReturn) {
+            // 储存全局变量
+            sessionStorage.setItem(`API_${rowIndex + '_' + cur.varName}`, selectRow[cur.keyName])
+          }
+        }
+      }
+      this.onChangeFormGetApi(rowIndex,editVmodel,flag)
       this.$emit('change')
       data.required && (data.valid = this.checkData(data))
       if (this.config && this.config.dbLinkId && this.primaryRow && this.primaryRow[this.config.relationConf.primaryModel]) {
@@ -731,13 +749,21 @@ export default {
       }
       this.$emit('change')
     },
-    addRow(val, flag,relationFlag,index) {
+    /**
+     * val添加的行数据
+     * flag relationFlag关联别的数据传来的
+     * index 子表数据行数
+     * createFlag 创建时是否调用修改change方法
+     */
+    addRow(val, flag,relationFlag,index,createFlag) {
       this.isAddRow = true
       if (!Array.isArray(this.tableFormData)) {
         this.tableFormData = []
       }
       this.tableFormData.unshift(this.getEmptyRow(val, flag,relationFlag))
-      this.clearAddRowFlag(index)
+      if (!createFlag){
+        this.clearAddRowFlag(index)
+      }
     },
     getCmpValOfRow(row, key) {
       // 获取数字相关组件的输入值