Vue.js 中获取选中行 chiId 失败的解决方案

问题描述:

在 Vue.js 代码中,使用handleUpdate方法获取选中行的chiId时,出现错误r: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: 'undefined'

代码示例:

/** 修改按钮操作 */
     handleUpdate(row) {
       console.log(this.selection, 'sssss');
        if (!this.selection || this.selection.length === 0) {
         this.$message.warning('请先选中要修改的数据项');
         return;
       }
        const id = row ? row.chiId : this.selection[0].chiId;
        // if (isNaN(id)) {
       //   this.$message.warning('请先选中要修改的数据项');
       //   return;
       // }
       getReportingCmHealthInsure(id).then((response) => {
         this.data = response.data;
         this.acTaxPiIncomeReportingMedicalInsuranceList =           response.data.acTaxPiIncomeReportingMedicalInsuranceList;
         // this.$nextTick(() => {
         //   if (this.$refs.submitDialog) {
         //     this.$refs.submitDialog.handleWrite.call(this, row);
         //   } else {
         //     console.error('submitDialog ref is not found');
         //   }
         // });
          this.open = true;
         this.title = '商业健康保险明细编辑';
       });
     },
      handleSelectionChange(selection) {
       this.selection = selection;
       console.log(selection, 'selectionsss');
     },

原因分析:

根据给出的代码,拿不到选中的那一行的chiId的原因可能是因为row参数没有正确传递或者传递的值不正确。

handleUpdate方法中,首先判断row是否存在,如果存在则使用row.chiId作为id的值,否则使用this.selection[0].chiId作为id的值。这意味着,如果row不存在且this.selection也不存在或为空数组,就会报错并返回。

解决方案:

你需要确保row参数正确传递,并且this.selection数组中至少有一个元素,才能正确获取到选中的那一行的chiId值。

具体方法:

  1. 检查row参数:

    • 确保row参数在调用handleUpdate方法时被正确传递。
    • 检查row对象中是否包含chiId属性,以及chiId属性的值是否为有效数字类型。
  2. 检查this.selection数组:

    • 确保this.selection数组中至少包含一个元素。
    • 检查this.selection[0]对象中是否包含chiId属性,以及chiId属性的值是否为有效数字类型。

示例:

/** 修改按钮操作 */
     handleUpdate(row) {
       console.log(this.selection, 'sssss');
       if (!this.selection || this.selection.length === 0) {
         this.$message.warning('请先选中要修改的数据项');
         return;
       }
       // 获取选中的第一行的 chiId
       const id = row ? row.chiId : this.selection[0].chiId;
       if (typeof id === 'undefined' || isNaN(id)) {
         this.$message.warning('请先选中要修改的数据项');
         return;
       }
       getReportingCmHealthInsure(id).then((response) => {
         this.data = response.data;
         this.acTaxPiIncomeReportingMedicalInsuranceList =           response.data.acTaxPiIncomeReportingMedicalInsuranceList;
         this.open = true;
         this.title = '商业健康保险明细编辑';
       });
     },
      handleSelectionChange(selection) {
       this.selection = selection;
       console.log(selection, 'selectionsss');
     },

总结:

出现r: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: 'undefined'错误的原因是id的值为undefined,而getReportingCmHealthInsure方法期望的是一个数字类型的参数。

通过检查row参数和this.selection数组,确保id的值是有效的数字类型,就可以解决这个问题。


原文地址: https://www.cveoy.top/t/topic/pkoH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录