微信小程序?qū)崿F(xiàn)購(gòu)物車代碼實(shí)例詳解
其實(shí)購(gòu)物車都是類似的實(shí)現(xiàn)方法,只不過小程序是有他的數(shù)據(jù)層和業(yè)務(wù)層,在這里把之前的做法記錄一下,分享出來也希望能給需要的小伙伴帶來參考價(jià)值在最開始的時(shí)候先從本地存儲(chǔ)中獲取購(gòu)物車數(shù)據(jù),因?yàn)槲覀儠?huì)切換頁(yè)面 在頁(yè)面切換的過程中需要實(shí)時(shí)重新加載購(gòu)物車的數(shù)據(jù),所以我們把獲取的方法寫在onShow中,而不是onLoad中:
onShow: function () { const cart = wx.getStorageSync("cart"); let address = wx.getStorageSync("address") ; console.log(address); this.setData({ address, cart }) this.loadCarts(); this.countAll(); }
點(diǎn)擊按鈕更改購(gòu)物車的數(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: '您確定要?jiǎng)h除嗎', 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: '沒有庫(kù)存了', icon: 'none', duration: 1500, mask: true }); } this.loadCarts(); this.countAll(); }
加載購(gòu)物車數(shù)據(jù)的方法:
data: { cart: {}, address: {}, totalPrice: 0, categoryLength: 0, isAllChecked: true },
單個(gè)商品被選中時(shí)觸發(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 }) },
點(diǎn)擊結(jié)算時(shí)觸發(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(); },
純js代碼,可能有一定小程序代碼經(jīng)驗(yàn)的會(huì)看得輕松一點(diǎn)。以上便可以實(shí)現(xiàn)在對(duì)購(gòu)物車的商品進(jìn)行加減和全選與反選,以及對(duì)商品進(jìn)行數(shù)量合計(jì)并計(jì)算價(jià)格。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序 購(gòu)物車簡(jiǎn)單實(shí)例
- 微信小程序?qū)崙?zhàn)篇之購(gòu)物車的實(shí)現(xiàn)代碼示例
- 微信小程序?qū)崿F(xiàn)購(gòu)物車功能
- 微信小程序?qū)崿F(xiàn)多選框全選與反全選及購(gòu)物車中刪除選中的商品功能
- 微信小程序之購(gòu)物車功能
- 微信小程序購(gòu)物車、父子組件傳值及calc的注意事項(xiàng)總結(jié)
- 微信小程序?qū)崿F(xiàn)簡(jiǎn)單購(gòu)物車功能
- 微信小程序利用swiper+css實(shí)現(xiàn)購(gòu)物車商品刪除功能
- 微信小程序?qū)崿F(xiàn)加入購(gòu)物車滑動(dòng)軌跡
- 微信小程序?qū)崿F(xiàn)簡(jiǎn)單購(gòu)物車小功能
相關(guān)文章
微信小程序?qū)崿F(xiàn)列表頁(yè)的點(diǎn)贊和取消點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)列表頁(yè)的點(diǎn)贊和取消點(diǎn)贊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Javascript連接數(shù)據(jù)庫(kù)查詢并插入數(shù)據(jù)
這篇文章主要介紹了Javascript連接數(shù)據(jù)庫(kù)查詢并插入數(shù)據(jù),下面文章圍繞主題展開詳細(xì)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-03-03IScroll那些事_當(dāng)內(nèi)容不足時(shí)下拉刷新的解決方法
下面小編就為大家?guī)硪黄狪Scroll那些事_當(dāng)內(nèi)容不足時(shí)下拉刷新的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07js離開或刷新頁(yè)面檢測(cè)(且兼容FF,IE,Chrome)
這篇文章主要介紹了js離開或刷新頁(yè)面檢測(cè)(且兼容FF,IE,Chrome)。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03簡(jiǎn)單實(shí)現(xiàn)js倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了js倒計(jì)時(shí)效果的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02js實(shí)現(xiàn)點(diǎn)擊圖片將圖片地址復(fù)制到粘貼板的方法
這篇文章主要介紹了js實(shí)現(xiàn)點(diǎn)擊圖片將圖片地址復(fù)制到粘貼板的方法,涉及js操作節(jié)點(diǎn)的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02JavaScript ECMA-262-3 深入解析(一):執(zhí)行上下文實(shí)例分析
這篇文章主要介紹了JavaScript ECMA-262-3 執(zhí)行上下文,結(jié)合實(shí)例形式詳細(xì)分析JavaScript ECMA執(zhí)行上下文相關(guān)概念、原理與操作注意事項(xiàng),需要的朋友可以參考下2020-04-04