新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
如何在微信小程序中实现一个购物车功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了和政免费建站欢迎大家使用!
onShow: function () { const cart = wx.getStorageSync("cart"); let address = wx.getStorageSync("address") ; console.log(address); this.setData({ address, cart }) this.loadCarts(); this.countAll(); }
点击按钮更改购物车的数量:
handleNumEdit(e) { const { operator, goodsid } = e.target.dataset; let { cart } = this.data; cart[goodsid].count += (+operator); if (cart[goodsid].count < 1) { cart[goodsid].count = 1; wx.showModal({ title: '提示', content: '您确定要删除吗', showCancel: true, cancelText: '取消', cancelColor: '#000000', confirmText: '确定', confirmColor: '#3CC51F', success: (result) => { if (result.confirm) { delete cart[goodsid]; this.loadCarts(); this.countAll(); } else { } } }); } else if (cart[goodsid].count > cart[goodsid].goods_number) { cart[goodsid].count = cart[goodsid].goods_number; wx.showToast({ title: '没有库存了', icon: 'none', duration: 1500, mask: true }); } this.loadCarts(); this.countAll(); }
加载购物车数据的方法:
data: { cart: {}, address: {}, totalPrice: 0, categoryLength: 0, isAllChecked: true },
单个商品被选中时触发:
loadCarts() { let { cart } = this.data; let isAllChecked = true; for (const key in cart) { if (cart.hasOwnProperty(key)) { const element = cart[key]; if (!element.isChecked) { isAllChecked = false; break; } } } this.setData({ cart, isAllChecked }); },
全选和反选触发的事件:
handleItemChecked(e) { let { goodsid } = e.target.dataset; let { cart } = this.data; let { isChecked } = cart[goodsid]; cart[goodsid].isChecked = !isChecked; let checkedLength = 0; for (const key in cart) { if (cart.hasOwnProperty(key)) { if (cart[key].isChecked) { checkedLength++; } } } const isAllChecked = checkedLength == Object.keys(cart).length; this.countAll(); this.setData({ isAllChecked }) },
点击结算时触发:
handleItemAllChecked() { let { isAllChecked } = this.data; let { cart } = this.data; isAllChecked = !isAllChecked; for (const key in cart) { if (cart.hasOwnProperty(key)) { cart[key].isChecked = isAllChecked; } } this.setData({ isAllChecked, cart }) this.countAll(); },
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。