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

你可能感兴趣的文章
Vue过渡 & 动画---vue工作笔记0014
查看>>
Netty 异步任务调度与异步线程池
查看>>
Netty 的 Handler 链调用机制
查看>>
Netty 编解码器详解
查看>>
Netty 解决TCP粘包/半包使用
查看>>
Netty 调用,效率这么低还用啥?
查看>>
Netty 高性能架构设计
查看>>
Netty+Protostuff实现单机压测秒级接收35万个对象实践经验分享
查看>>
Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
查看>>
netty--helloword程序
查看>>
netty2---服务端和客户端
查看>>
Netty5.x 和3.x、4.x的区别及注意事项(官方翻译)
查看>>
netty——bytebuf的创建、内存分配与池化、组成、扩容规则、写入读取、内存回收、零拷贝
查看>>
netty——Channl的常用方法、ChannelFuture、CloseFuture
查看>>
netty——EventLoop概念、处理普通任务定时任务、处理io事件、EventLoopGroup
查看>>
netty——Future和Promise的使用 线程间的通信
查看>>
netty——Handler和pipeline
查看>>
Vue输出HTML
查看>>
netty——黏包半包的解决方案、滑动窗口的概念
查看>>
Netty中Http客户端、服务端的编解码器
查看>>