博客
关于我
微信小程序(优化)——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/

你可能感兴趣的文章
NAS、SAN和DAS的区别
查看>>
NAS个人云存储服务器搭建
查看>>
NAT PAT故障排除实战指南:从原理到技巧的深度探索
查看>>
nat 网卡间数据包转发_你是不是从来没有了解过光纤网卡,它跟普通网卡有什么区别?...
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,解决动态域名解析难题
查看>>
natapp搭建外网服务器
查看>>
NativePHP:使用PHP构建跨平台桌面应用的新框架
查看>>
Native方式运行Fabric(非Docker方式)
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>
nat打洞原理和实现
查看>>
NAT技术
查看>>
NAT模式/路由模式/全路由模式 (转)
查看>>
NAT模式下虚拟机centOs和主机ping不通解决方法
查看>>
NAT的两种模式SNAT和DNAT,到底有啥区别?
查看>>
NAT的全然分析及其UDP穿透的全然解决方式
查看>>
NAT类型与NAT模型详解
查看>>
NAT网络地址转换配置实战
查看>>