微信小程序 动态修改数组数据方法

数据demo:

tabListLocal: [ // typeLocal主要是和后台数据进行对应匹配
      { currentTab: 0, title: '全部',   typeLocal:0,  total: 0, modelCode:'allReports',list:[] },
      { currentTab: 1, title: '待分配', typeLocal: 7, total: 0, modelCode: 'tobeAssign', list: [] },
      { currentTab: 2, title: '待接单', typeLocal: 8, total: 0, modelCode: 'tobeAccept', list: [] },
      { currentTab: 3, title: '待处理', typeLocal: 9, total: 0, modelCode: 'tobeHandle', list: [] },
      { currentTab: 4, title: '待评论', typeLocal: 10, total: 0, modelCode: 'tobeAssess', list: []}],

由于list的数据是动态的,需要接口查询返回,因此需要遍历数据,并动态修改数据。

根据不同的状态去遍历查询该状态下的数据列表,把返回的接口数据赋值到list属性。

 this.data.myWorkOrderList.forEach((item,index) =>{
      getRepairOrderList(item.typeLocal).then(rsp => {
        if (rsp.status === 200) {
          this.setData({
            [`myWorkOrderList[${index}].list`] : rsp.data
          })
        }
      })
    })

动态赋值方法有2种:

方式一:使用es6语法

[`myWorkOrderList[${index}].list`] : rsp.data

方式二:整体赋值

 ……意思就是,先修改值,再重新setData一次让其生效。(暂未用过)

猜你喜欢

转载自www.cnblogs.com/luoxuemei/p/12767610.html