//js
add:function(res){const db = wx.cloud.database();var needId = res.currentTarget.dataset.id;//更新数据库就是根据这个id改的,每个记录都有一个唯一的_id
console.log("Hello")var Num = res.currentTarget.dataset.num+1;var id =parseInt(res.currentTarget.id);var up ="goods["+id+"].num"this.setData({[up]:parseInt(Num)})
db.collection("goods").doc(needId).update({
data:{
num: Num
}}),this.getTotalprice();}
reduce:function(res){const db = wx.cloud.database();var needId = res.currentTarget.dataset.id;
console.log("Hello")var Num = res.currentTarget.dataset.num-1;if(Num <0) Num =0;var id =parseInt(res.currentTarget.id);var up ="goods["+id+"].num"this.setData({[up]:parseInt(Num)})
db.collection("goods").doc(needId).update({
data:{
num: Num
}})this.getTotalprice();}
减函数和加函数区别不大,就是加一个特判(商品数不能小于0)
看看买得起吗(求总金额)
getTotalprice:function(){var info =this.data.goods;var totalPrice =0;for(let i =0; i < info.length; i++){// info[i].vis是,是否选了这个商品;选了加进去if(info[i].vis){
totalPrice += info[i].num*info[i].price;}}this.setData({
totalPrice: totalPrice.toFixed(2),
goods: info
})},
全选
selectALL:function(){var selectAll =this.data.selectAll;
selectAll =!selectAll;//是现在状态的相反值,已经全选=》都不选 or 全选var info =this.data.goods;for(let i =0; i < info.length; i++){
info[i].vis = selectAll;}this.setData({
goods: info,
selectAll: selectAll
})this.getTotalprice();}