您好,登錄后才能下訂單哦!
如何在微信小程序中實現(xiàn)一個購物車功能?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
onShow: function () { const cart = wx.getStorageSync("cart"); let address = wx.getStorageSync("address") ; console.log(address); this.setData({ address, cart }) this.loadCarts(); this.countAll(); }
點擊按鈕更改購物車的數(shù)量:
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(); }
加載購物車數(shù)據(jù)的方法:
data: { cart: {}, address: {}, totalPrice: 0, categoryLength: 0, isAllChecked: true },
單個商品被選中時觸發(fā):
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 }); },
全選和反選觸發(fā)的事件:
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 }) },
點擊結(jié)算時觸發(fā):
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(); },
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。