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

你可能感兴趣的文章
NC综合漏洞利用工具
查看>>
ndarray 比 recarray 访问快吗?
查看>>
ndk-cmake
查看>>
NdkBootPicker 使用与安装指南
查看>>
ndk特定版本下载
查看>>
NDK编译错误expected specifier-qualifier-list before...
查看>>
Neat Stuff to Do in List Controls Using Custom Draw
查看>>
Necurs僵尸网络攻击美国金融机构 利用Trickbot银行木马窃取账户信息和欺诈
查看>>
Needle in a haystack: efficient storage of billions of photos 【转】
查看>>
NeHe OpenGL教程 07 纹理过滤、应用光照
查看>>
NeHe OpenGL教程 第四十四课:3D光晕
查看>>
Neighbor2Neighbor 开源项目教程
查看>>
neo4j图形数据库Java应用
查看>>
Neo4j图数据库_web页面关闭登录实现免登陆访问_常用的cypher语句_删除_查询_创建关系图谱---Neo4j图数据库工作笔记0013
查看>>
Neo4j图数据库的介绍_图数据库结构_节点_关系_属性_数据---Neo4j图数据库工作笔记0001
查看>>
Neo4j图数据库的数据模型_包括节点_属性_数据_关系---Neo4j图数据库工作笔记0002
查看>>
Neo4j安装部署及使用
查看>>
Neo4j电影关系图Cypher
查看>>
Neo4j的安装与使用
查看>>
Neo4j(1):图数据库Neo4j介绍
查看>>