博客
关于我
微信小程序(优化)——this.setData()动态修改数组中的某一值
阅读量:154 次
发布时间:2019-02-28

本文共 877 字,大约阅读时间需要 2 分钟。

wxml代码

info="{

{item.num==0?'':item.num}}"为三元运算符,当{
{item.num}}
为0是不显示,不为0时显示
data-index='{
{index}}'
为传过去的数据索引值

data中的数据

data:{  goodsList: [    {      _id:1,      title: "澳洲原切牛排",      desc: "一场精致的烛光晚餐从它开始",      price: "8.00",      imgUrl: "../../static/images/rou.png",      num:0      // 需要改变的数据    }, {      _id:2,      title: "安吉散养土鸡蛋",      desc: "鲜嫩的口味,把田园的清新带给你",      price: "8.00",      imgUrl: "../../static/images/dan.png",      num: 1      // 需要改变的数据    }  ]}

数组对象中的 num 为需要修改的值

js函数代码

add:function(e){    let id = e.currentTarget.dataset.id // 获取传入的id    let index = e.currentTarget.dataset.index  // 获取数据的索引    let temp = 'goodsList[' + index +'].num'  // 获取goodsList[index].num    this.setData({      [temp]:this.data.goodsList[index].num + 1    })  }

let temp = 'goodsList[' + index +'].num'为主要代码,打印出的数据为goodsList[index].num

注:index为变量,根据传入的索引值而改变

转载地址:http://vmad.baihongyu.com/

你可能感兴趣的文章
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>