小程序新增、删除数组以及图片上传问题

本文详细介绍了在小程序中实现垫款功能的优化过程,包括如何处理单据照片上传、输入框值的动态更新、新增与删除操作的逻辑,以及表单验证和数据提交的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<view class='prepay'>
<form bindsubmit="formSubmit" bindreset="formReset">
<view class='dd' wx:for="{{list.orderDetailPrepayDOList}}" wx:for-index="index" wx:key="index">
  <view class='prepay-main'>订单号:<view class='num' name="{{id}}">{{id}}</view></view>
  <view class='prepay-main'>垫款项目:<input type='text' data-index="{{index}}" bindinput='nameTab' name="name{{index}}" value='{{inputVal[index]}}' placeholder="请输入垫款项目"></input></view>
   <view class='prepay-main1'>单据照片:
  <view class='prepay-img'><image src='{{imge[index]}}' data-index="{{index}}" bindtap='img' name="{{imgs}} " mode="aspecFill"></image></view>
  </view >
  <view class='prepay-main'>垫付款:
  <input type='number' data-index="{{index}}" bindinput='setModal'  maxlength="10" name="money{{index}}"  value='{{moneyVal[index]}}' placeholder="请输入垫付款金额" bindinput='moneyTab'></input>
  </view> 
    <button class='delButton' bindtap='del' data-index="{{index}}" wx:if="{{list.orderDetailPrepayDOList.length >1}}">删除</button>
     <button class='delButton' disabled  wx:else>删除</button>
</view>
  <view class='prepay-main'>   
  <button  class='button' disabled bindtap='addPrepay' wx:if="{{list.orderDetailPrepayDOList.length >2}}" data-index="{{index}}">新增</button>
  <button  class='button'  bindtap='addPrepay' wx:else>新增</button>
  <button  class='button'  bindtap='skipPrepay'>跳过</button>
  <button  class='button'  form-type='submit'>保存</button>
  <button  class='button'  bindtap='cancely'>取消</button>
  </view>
  </form>
</view>

 

data: {
    imge: ['../../../../images/upload.png'],
    id:'',
    list: {
      orderDetailPrepayDOList: [
        {
          img: '',
          money:'',
          name:''
        }
      ],
    },
    imgs:[],
    imgs0:"",
    imgs1: "",
    imgs2: "",
    inputVal:[],
    moneyVal:[],
    array:{},
    arrayMoney:{},
    arrayImgs:{}
  },
// 单据照片
  img: function (e) {
    var imag=e.currentTarget.dataset.index
    var imge = this.data.imge;
    this.setData({
      imag: imag
            })
    var that = this;
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        imge[imag] = res.tempFilePaths;
        that.setData({
          imge: imge
          })
        var front = res.tempFilePaths;
        api.uploadRequest(api.apiUrl.UPLOAD_TMP_POST, front[0], null, function (res) {
          var imgs = JSON.parse(res.data).data
          if (imag == 0) {
            that.setData({
              imgs0: imgs
            })
            imge.push(that.data.imgs0)
          } else if (imag == 1) {
            that.setData({
              imgs1: imgs
            })
            imge.push(that.data.imgs1)
          } else if (imag == 2) {
            that.setData({
              imgs2: imgs
            })
            imge.push(that.data.imgs2)
          }
          var arrayImgs = that.data.arrayImgs
          arrayImgs[imag] = imge
          that.setData({
            imgs: imge,
            arrayImgs: arrayImgs
          })
          console.log('显示的图片', that.data.imge)
          console.log('返回的图片', that.data.imgs)
        });
      }
    })
  },
  formSubmit: function (e){
    console.log('表单value值',e.detail.value)
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showToast(error)
      return false
    }
    var that = this
    console.log('第一张图片', that.data.imgs0)
    console.log('第二张图片', that.data.imgs1)
    console.log('第三张图片', that.data.imgs2)
    var orderList = that.data.list.orderDetailPrepayDOList
    for (var i = 0; i < orderList.length; i++) {
      console.log(orderList[i]);//遍历输出
      if (i == 0) {
        var name = e.detail.value.name0
        var money = e.detail.value.money0
        orderList[i].img = that.data.imgs0,
          orderList[i].money = money,
          orderList[i].name = name
      } else if (i == 1) {
        var name = e.detail.value.name1
        var money = e.detail.value.money1
        orderList[i].img = that.data.imgs1,
          orderList[i].money = money,
          orderList[i].name = name
      }
      else if (i == 2) {
        var name = e.detail.value.name2
        var money = e.detail.value.money2
        orderList[i].img = that.data.imgs2,
          orderList[i].money = money,
          orderList[i].name = name
      }
    }
    console.log("最后上传数据",that.data.list)
    api.putRequest('/weChat/orders/' + that.data.id + '/prepay.do', that.data.list, function (res) {
      that.showToast1()
      setTimeout(function () {
        wx.navigateBack({
          delta: 2
        })
      }, 2000)
    })
  },
//获取输入框的值
nameTab:function(e){
    var value = e.detail.value
    var index = e.currentTarget.dataset.index;
    var nameVal = this.data.inputVal;
    nameVal[index] = value;//修改对应索引值的内容
    var array = this.data.array
    array[index] = nameVal
    this.setData({
      inputVal: nameVal,
      array: array
    })
    console.log("垫款项目", this.data.inputVal)
    console.log("垫款项目", this.data.array)
  },
  moneyTab:function(e){
    var values = e.detail.value
    var index = e.currentTarget.dataset.index;
    var moneyVal = this.data.moneyVal;
    var arrayMoney = this.data.arrayMoney
    arrayMoney[index] = moneyVal
    moneyVal[index] = values;
    this.setData({
      moneyVal: moneyVal,
      arrayMoney: arrayMoney
    })
    console.log("垫付款", this.data.moneyVal)
  },
 // 新增
  addPrepay:function(e){
    let list = this.data.list;
    var inputVal =this.data.inputVal
    var moneyVal = this.data.moneyVal;
    var imge = this.data.imge;
    var imgs = this.data.imgs;
    var arrayImgs = this.data.arrayImgs;
    imge.push('../../../../images/upload.png')
    this.setData({
        imge: imge
    })
    if (inputVal[0] == null || arrayImgs[0] == null || moneyVal[0] == null){
      wx.showModal({
        title: '提示',
        content: '垫款项目、单据照片、垫款金额不能为空',
        showCancel: false
      })
    } else if (list.orderDetailPrepayDOList.length==2&&(inputVal[1] == null || arrayImgs[1] == null || moneyVal[1] == null)) {
      wx.showModal({
        title: '提示',
        content: '垫款项目、单据照片、垫款金额不能为空',
        showCancel: false
      })
    }
    else{
      list.orderDetailPrepayDOList.push(
        {
          img: '',
          money: '',
          name:''
        }
      )
        this.setData({
        list: list
      })
    }
  },// 新增
  addPrepay:function(e){
    let list = this.data.list;
    var inputVal =this.data.inputVal
    var moneyVal = this.data.moneyVal;
    var imge = this.data.imge;
    var imgs = this.data.imgs;
    var arrayImgs = this.data.arrayImgs;
    imge.push('../../../../images/upload.png')
    this.setData({
        imge: imge
    })
    if (inputVal[0] == null || arrayImgs[0] == null || moneyVal[0] == null){
      wx.showModal({
        title: '提示',
        content: '垫款项目、单据照片、垫款金额不能为空',
        showCancel: false
      })
    } else if (list.orderDetailPrepayDOList.length==2&&(inputVal[1] == null || arrayImgs[1] == null || moneyVal[1] == null)) {
      wx.showModal({
        title: '提示',
        content: '垫款项目、单据照片、垫款金额不能为空',
        showCancel: false
      })
    }
    else{
      list.orderDetailPrepayDOList.push(
        {
          img: '',
          money: '',
          name:''
        }
      )
        this.setData({
        list: list
      })
    }
  },
// 跳过
  skipPrepay:function(){
    var that = this;
    var skipData = {
      orderNo: '',
      dto: {},
    }
    api.putRequest('/weChat/orders/' + that.data.id + '/prepay.do', skipData, function (res) {
      console.log('submit success');
      wx.navigateTo({
        url: '../receipt/receipt?pid=' + that.data.id
      })
    })
  },
  // 删除
  del:function(e){
    var that = this
    var index = e.currentTarget.dataset.index
    var oldInputVal = that.data.inputVal;
    var list = that.data.list;
    var moneyVal = that.data.moneyVal;
    var imge = this.data.imge
    var imgs = this.data.imgs
    var array = this.data.array
    var arrayMoney = this.data.arrayMoney
    var arrayImgs = this.data.arrayImgs
    wx.showModal({
      title: '提示',
      content: '确定删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定',list)
          list.orderDetailPrepayDOList.splice(index, 1);
          oldInputVal.splice(index, 1);
          moneyVal.splice(index, 1);
          imge.splice(index,1)
          imgs.splice(index, 1)
          delete (array[index]);
          delete (arrayMoney[index]);
          delete (arrayImgs[index]);
            if (index == 0) {
              that.setData({
                imgs0: that.data.imgs1,
                imgs1: that.data.imgs2,
                imgs2: '',
              })
            } else if (index == 1) {
              that.setData({
                imgs1: that.data.imgs2,
                imgs2: '',
              })
            }
          if (list.orderDetailPrepayDOList.length < 1) {
            list.orderDetailPrepayDOList = [
              {
                img: '',
                money: '',
                name: ''
              }
            ]
          }
          that.setData({
            list: list,
            inputVal: oldInputVal,
            moneyVal: moneyVal,
            imge: imge
          })
          console.log('删除后显示', array)
          console.log('删除后显示的图片', imge)
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  // 取消
  cancely: function () {
    wx.navigateBack({
      delta: 2
    })
  }

因为图片设置的是上传一张就调用一次接口,并将返回的图片存入到数组,最后赋值提交.但每添加一个图片就上传成功了 即使删除了显示图片,但上传成功的图片依然存在,所以我将上传成功的图片用键值对的方式存入,再根据key只删除,判断下标然后赋值,这样提交时就是对应的了

提交时要判断是否为空,所以我将输入的值也存入键值对,再判断是否为空以及利用数组长度判断新增后的数据是否为空

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值