您好,登錄后才能下訂單哦!
如何在微信小程序中實現一個購物車功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
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(); },
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。