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

你可能感兴趣的文章
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>
mysql5.5最简安装教程
查看>>
mysql5.6 TIME,DATETIME,TIMESTAMP
查看>>
mysql5.6.21重置数据库的root密码
查看>>
Mysql5.6主从复制-基于binlog
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>