表单点击提交的时候,选项内容会变成一瞬间的其他文案【REACT】

表单点击提交的时候,选项内容会变成一瞬间的其他文案

原码如下

dataFormSubmit() {
const formerUnionBranchInfoList = this.dataForm.unionBranchInfoList;
      // eslint-disable-next-line max-len
      const currentUnionBranchInfoList = formerUnionBranchInfoList.map((item) => ({ branchCode: item, ...this.availableUnionBranches.find((i) => i.branchCode === item) }));
      console.log(currentUnionBranchInfoList,'currentUnionBranchInfoList');
      this.dataForm.unionBranchInfoList = currentUnionBranchInfoList;
      if (!this.dataForm.unionBranchInfoSwitch) {
        this.dataForm.unionBranchInfoList = [];
      }
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          insertOrUpdate(this.dataForm).then((res) => {
            if (res && res.data) {
              this.$message.success(`${this.type === 'add' ? '添加' : '更新'}成功`);
              this.$emit('refresh');
            } else {
              this.$message.error(res.msg);
            }
          }).finally(() => {
            this.loading = false;
          });
        }
      });
      }

问题出在 dataFormSubmit 方法中对 unionBranchInfoList 的处理上。当前的处理方式会直接修改 dataForm 中的数据,这可能导致视图上的临时变化。

修改后的代码

// ... existing code ...
dataFormSubmit() {
  this.$refs.dataForm.validate((valid) => {
    if (valid) {
      this.loading = true;
      // 创建提交数据的副本,避免修改原始数据
      const submitData = { ...this.dataForm };
      
      if (submitData.unionBranchInfoSwitch) {
        const unionBranchInfoList = submitData.unionBranchInfoList.map((item) => ({
          branchCode: item,
          ...this.availableUnionBranches.find((i) => i.branchCode === item)
        }));
        submitData.unionBranchInfoList = unionBranchInfoList;
      } else {
        submitData.unionBranchInfoList = [];
      }

      insertOrUpdate(submitData).then((res) => {
        if (res && res.data) {
          this.$message.success(`${this.type === 'add' ? '添加' : '更新'}成功`);
          this.$emit('refresh');
        } else {
          this.$message.error(res.msg);
        }
      }).finally(() => {
        this.loading = false;
      });
    }
  });
},
// ... existing code ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值