国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

11、微信小程序——購物車

這篇具有很好參考價值的文章主要介紹了11、微信小程序——購物車。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

1、代碼分析

1.1? dom結(jié)構(gòu)分析

1.2? 全選控制反選

1.3? 反選控制全選

1.4、計算總價

1.4 加、減

2、效果、代碼演示?

11、微信小程序——購物車

1、代碼分析

1.1? dom結(jié)構(gòu)分析

購物車的組件結(jié)構(gòu)主要由兩部分組成:多選框部分( checkbox-group ) + 全選框部分( view )

11、微信小程序——購物車

1.2? 全選控制反選

業(yè)務邏輯:

1、全選框(選中)——》 多選框(選中)、全選框(不選中)——》 多選框(不選中)

2、令復選框跟隨全選框的狀態(tài)變化——遍歷數(shù)據(jù),設置每條數(shù)據(jù)的 isChecked 狀態(tài)

注意:因為無法直接操作數(shù)據(jù),所以需要先行拷貝數(shù)據(jù),再對其進行遍歷(在本項目中,所有需要對數(shù)據(jù)進行遍歷、篩選操作的步驟,都需要先拷貝數(shù)據(jù))

 allChecked(e){
        console.log(e);  //觸發(fā)點擊事件,拿到全選框的事件源對象
        // 全選框: 狀態(tài)為 true時,令其為 false,當狀態(tài)為 false時,令其狀態(tài)變?yōu)?true
        if(this.data.isAllChecked){
        // 遍歷數(shù)據(jù),令復選框的狀態(tài)跟隨全選框變化
        // 拷貝數(shù)據(jù)、遍歷數(shù)據(jù)
        let goodslist=JSON.parse(JSON.stringify(this.data.goods))
        goodslist.forEach(item=>item.isChecked=false)
            this.setData({
                isAllChecked:false,
                goods:goodslist  //重新渲染
            })
        }else{
            let goodslist=JSON.parse(JSON.stringify(this.data.goods))
            goodslist.forEach(item=>item.isChecked=true)
            this.setData({
                isAllChecked:true,
                goods:goodslist
            })
        }
    },

1.3? 反選控制全選

業(yè)務邏輯:

1、多選框(全部選中)——》全選框(選中)

因為復選框有多個,全選框只有一個,所以多選框控制全選框被選中的前提是確定每一個多選框都被選中了? ,打印復選框的事件源對象,發(fā)現(xiàn)復選框有一 detail.value 屬性,可顯示?復選框為 true 的對象 / 數(shù)量。所以當 e.detail.value.length===this.data.goods.length 時,表示復選框都被選中了

11、微信小程序——購物車

?2、多選框(有一不選中)——》全選框(不選中)

遍歷數(shù)據(jù)( forEach ) 并 檢查元素 ( includes ),如果?e.detail.value 中包含 數(shù)據(jù)id,則令該復選框的 isChecked=true,否則為 false

 // 單選控制全選
    changeCound(e) {
        console.log(e);
        let checkedArr = e.detail.value
        if (checkedArr.length == this.data.goods.length) {
            let goodsList = JSON.parse(JSON.stringify(this.data.goods))
            goodsList.forEach(item => item.isChecked = true)
            this.setData({
                isAllChecked: true,
                goods: goodsList
            })
        } else {
            let goodsList = JSON.parse(JSON.stringify(this.data.goods))
            goodsList.forEach(item => {
                if (checkedArr.includes(item.id)) {
                    item.isChecked = true
                } else {
                    item.isChecked = false
                }
            })
            this.setData({
                isAllChecked: false,
                goods: goodsList
            })
        }
    },

1.4、計算總價

通常用 reduce 來計算總價,其中 total 表示舊值,item表示新值,0為初始值

. reduce( ( total , item ) => { } , 0 )

 // 計算總價
    computedTotalPrice() {
        let totalPrice = this.data.goods.reduce((total, item) => {
            if (item.isChecked) {
                return total + item.num * item.price
            } else {
                return total
            }
        }, 0)
        this.setData({
            totalPrice: totalPrice
        })
    },

1.4 加、減

11、微信小程序——購物車

 // 加、減、刪除
    handle: function (e) {
        let id = e.currentTarget.dataset.id
        console.log(id);
        var index = e.currentTarget.dataset.index
        var val = `goods[${index}].num`  //對數(shù)據(jù)對象 goods 中的 num 屬性進行修改
        let oldnum = this.data.goods.find(item => item.id == id).num //查找
        console.log(oldnum);
        // 減
        if (e.currentTarget.dataset.name == "mulse") {
            let newNum = oldnum - 1
            if (newNum > 0) {
                this.setData({
                    [val]: newNum
                })
            }else if(newNum == 0){
                let goodsList = JSON.parse(JSON.stringify(this.data.goods))
                let newGoodList=goodsList.filter(item=>item.id!==id)
                this.setData({
                    goods:newGoodList
                })
            }
            // 加
        }else if(e.currentTarget.dataset.name=="add"){
            let newnum=++oldnum
            this.setData({
                [val]:newnum
            })
        }  
          // 刪除
         else if (e.currentTarget.dataset.name == "del") {
              let goodslist = JSON.parse(JSON.stringify(this.data.lists))
              let newLists = goodslist.filter(item => item.id !== id)
              this.setData({
                  lists: newLists
              })
          }
        this.computedTotalPrice()
    }
})

2、代碼演示?

?index.wxml:

<checkbox-group bindchange="changeCound" class="top">
    <view wx:for="{{goods}}" wx:key="id" class="checkbox">
        <view class="chleft">
            <checkbox value="{{item.id}}" checked="{{item.isChecked}}" class="cleft"></checkbox>
            <image src="{{item.imgUrl}}" mode="" class="pic" />
        </view>
        <view class="Box">
            <view class="box-left">
                <text class="name">{{item.name}}</text>
                <text class="price">¥{{item.price}}</text>
            </view>
            <view class="box-right">
                <text bindtap="handle" data-id="{{item.id}}" data-name="mulse" data-index="{{index}}">-</text>
                <text>{{item.num}}</text>
                <text bindtap="handle" data-id="{{item.id}}" data-name="add" data-index="{{index}}">+</text>
                <text bindtap="handle" class="del">x</text>
            </view>
        </view>
    </view>
</checkbox-group>
<view class="bottom">
    <view class="bottom-box" bindtap="allChecked">
        <label>
            <checkbox value="" checked="{{isAllChecked}}" />
            <text>全選</text>
        </label>
    </view>
    <view class="total">
        {{totalPrice}} 元
    </view>
</view>

index.scss

.top {
    height:90vh;
    overflow: auto;
    .checkbox {
        position: relative;
        width: 100vw;
        height: 100px;
        border: 1px solid #eee;
        display: flex;
        flex-direction: row;
        .chleft{
            display: flex;
            flex-direction: row;
            align-content: center;
            width: 41%;
            .cleft {
                margin-left: 10px;
                margin-right: 10px;
                line-height: 100px;
            }
            .pic {
                width:80px;
                height: 80px;
                margin-top: 10px;
                object-fit: cover;
            }
        }
        .Box{
            width: 59%;
            height: 100%;
            background-color: #fff;
            .box-left{
                position: relative;
                width: 100%;
                height: 50%;
                text{
                    position: relative;
                    width:50px;
                    height: 20px;
                    margin-left: 10px;
                }
                .name{
                    position: absolute;
                    top:20rpx;
                    left:5rpx;
                }
                .price{
                    position: absolute;
                    top:20rpx;
                    right:5rpx;
                    text-align: left;
                }
            }
            .box-right{
                position: relative;
                margin-top: 17px;
                >text{
                    width: 20px;
                    margin-left: 10px;
                    font-weight: bolder;
                }
                .del{
                    position: absolute;
                    top:-2px;
                    right: 10px;
                    margin-left: 100px;
                }
            }
        }
    }
}
.bottom{
    position: fixed;
    bottom: 0;
    left:0;
    width: 100%;
    height: 11vh;
    box-shadow: 5px 5px 5px 5px rgb(204, 204, 204);
    line-height: 11vh;
    display: flex;
    flex-direction: row;
    .bottom-box{
        width: 50%;
        margin-left: 10px;
    }
    .total{
        width: 50%;
        text-align: right;
        padding-right: 15px;
    }
}

index.js文章來源地址http://www.zghlxwxcb.cn/news/detail-430843.html

Page({
    data: {
        goods: [
            {
                id: '1',
                name: '黃桃',
                num: 10,
                price: 10,
                isChecked: false,
                imgUrl: '/images/HT.jpg'
            },
            {
                id: '2',
                name: '西瓜',
                num: 20,
                price: 8,
                isChecked: true,
                imgUrl: '/images/XG.jpg'
            },
            {
                id: '3',
                name: '獼猴桃',
                num: 6,
                price: 12,
                isChecked: false,
                imgUrl: '/images/MHT.jpg'
            }
        ],
        isAllChecked: false,
        totalPrice: 0
    },
    onLoad() {
        this.computedTotalPrice()
    },
    // 單選控制全選
    changeCound(e) {
        console.log(e);
        let checkedArr = e.detail.value
        if (checkedArr.length == this.data.goods.length) {
            let goodsList = JSON.parse(JSON.stringify(this.data.goods))
            goodsList.forEach(item => item.isChecked = true)
            this.setData({
                isAllChecked: true,
                goods: goodsList
            })
        } else {
            let goodsList = JSON.parse(JSON.stringify(this.data.goods))
            goodsList.forEach(item => {
                if (checkedArr.includes(item.id)) {
                    item.isChecked = true
                } else {
                    item.isChecked = false
                }
            })
            this.setData({
                isAllChecked: false,
                goods: goodsList
            })
        }
        this.computedTotalPrice()

    },
    // 全選控制單選
    allChecked(e) {
        if (this.data.isAllChecked) {
            let goodsList = JSON.parse(JSON.stringify(this.data.goods))
            goodsList.forEach(item => item.isChecked = false)  //這里是賦值
            this.setData({
                isAllChecked: false,
                goods: goodsList
            })
        } else {
            let goodsList = JSON.parse(JSON.stringify(this.data.goods))
            goodsList.forEach(item => item.isChecked = true)  //這里是賦值
            this.setData({
                isAllChecked: true,
                goods: goodsList
            })
        }
        this.computedTotalPrice()
    },

    // 計算總價
    computedTotalPrice() {
        let totalPrice = this.data.goods.reduce((total, item) => {
            if (item.isChecked) {
                return total + item.num * item.price
            } else {
                return total
            }
        }, 0)
        this.setData({
            totalPrice: totalPrice
        })
    },

    // 加、減、刪除
    handle: function (e) {
        let id = e.currentTarget.dataset.id
        console.log(id);
        var index = e.currentTarget.dataset.index
        var val = `goods[${index}].num`
        let oldnum = this.data.goods.find(item => item.id == id).num
        console.log(oldnum);
        if (e.currentTarget.dataset.name == "mulse") {
            let newNum = oldnum - 1
            if (newNum > 0) {
                this.setData({
                    [val]: newNum
                })
            }else if(newNum == 0){
                let goodsList = JSON.parse(JSON.stringify(this.data.goods))
                let newGoodList=goodsList.filter(item=>item.id!==id)
                this.setData({
                    goods:newGoodList
                })
            }
        }else if(e.currentTarget.dataset.name=="add"){
            let newnum=++oldnum
            this.setData({
                [val]:newnum
            })
        }
        this.computedTotalPrice()
    }
})

到了這里,關(guān)于11、微信小程序——購物車的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)商品加入購物車案例

    微信小程序?qū)崿F(xiàn)商品加入購物車案例

    思考: 購物車中的數(shù)據(jù)保存在哪里?用哪種數(shù)據(jù)結(jié)構(gòu)進行保存? 小程序中可能有多個頁面需要對購物車中的數(shù)據(jù)進行操作,因此我們想到把數(shù)據(jù)存到全局中。可以使用 wx.setStorageSync() 儲存,用 wx.getStorageSync() 進行獲取,以數(shù)組格式方便對數(shù)據(jù)進行操作。 一、商品加入購物車

    2024年02月10日
    瀏覽(19)
  • 微信小程序商城開發(fā)-商品詳情頁跳轉(zhuǎn)購物車

    微信小程序商城開發(fā)-商品詳情頁跳轉(zhuǎn)購物車

    微信小程序商城開發(fā)中商品詳情頁中購物車頁面的跳轉(zhuǎn)(僅限于tabbar頁面的跳轉(zhuǎn)) ? 點擊商品詳情頁中的購物車圖標跳轉(zhuǎn)不到tabbar及導航欄中的購物車頁面,總是報錯 微信小程序開發(fā)需要跳轉(zhuǎn)tabbar頁面的話,有固定的跳轉(zhuǎn)方式,不是開發(fā)中所有的跳轉(zhuǎn)方式都適用于tabbar跳轉(zhuǎn)

    2024年02月17日
    瀏覽(23)
  • 【微信小程序】-- uni-app 項目-- 購物車 -- 首頁 - 輪播圖效果(五十二)

    【微信小程序】-- uni-app 項目-- 購物車 -- 首頁 - 輪播圖效果(五十二)

    ?? 所屬專欄:【微信小程序開發(fā)教程】 ?? 作??者:我是夜闌的狗?? ?? 個人簡介:一個正在努力學技術(shù)的CV工程師,專注基礎(chǔ)和實戰(zhàn)分享 ,歡迎咨詢! ?? 歡迎大家:這里是CSDN,我總結(jié)知識的地方,喜歡的話請三連,有問題請私信 ?? ?? ?? ??大家好,又見面了,

    2023年04月18日
    瀏覽(90)
  • 微信電商小程序購買/加入購物車組件設計

    微信電商小程序購買/加入購物車組件設計

    作為一名常常擺爛,一蹶不振的大學生,最近接到了開發(fā)電商小程序的小任務,既然是電商,總得有購買加車功能吧?經(jīng)過n個坤年的拜讀微信小程序開發(fā)者文檔還有別的大佬的指點,奉上我自己的理解,歡迎各位大佬指點改正,學習交流,共同進步。該文章適合微信小程序初

    2024年02月09日
    瀏覽(17)
  • 購物車程序?qū)崿F(xiàn)教程

    購物車程序?qū)崿F(xiàn)教程

    在本教程中,我們將實現(xiàn)一個購物車程序,實現(xiàn)在界面中以列表的形式顯示購物車的商品信息。商品信息包含商品名稱,價格和數(shù)量,并能實現(xiàn)對應的增刪改查操作。我們將使用 Android Studio 和 SQLite 數(shù)據(jù)庫來完成這個任務。 我們的購物車程序由以下四個主要類組成: MainAct

    2024年02月04日
    瀏覽(21)
  • 實現(xiàn)蛋糕商城購物車功能代碼實現(xiàn)實驗報告

    一、實驗目的 1、熟悉HttpSession接口中常用方法 2、熟悉Session 對象的生命周期 3、熟悉兩種方法返回與當前請求相關(guān)的HttpSession對象。 4、學會如何使用Session 技術(shù)模擬用戶登錄的功能 二、實驗內(nèi)容 實現(xiàn)購物車 1、在chapler05 項目下新建一個名稱為 cn.itcast.session.entity 的包,在該

    2024年02月09日
    瀏覽(41)
  • 商城小程序(8.購物車頁面)

    商城小程序(8.購物車頁面)

    本章主要完成pages下的cart購物頁面編寫 定義如下UI結(jié)構(gòu) 美化樣式 通過 mapState 輔助函數(shù),將Store中的cart數(shù)組映射到當前頁面中使用: 在UI結(jié)構(gòu)中,通過v-for渲染自定義組件my-goods 打開my-goods.vue組件,為商品左側(cè)圖片區(qū)域添加radio足跡 并美化UI ,使radio組件和image組件左右布局

    2024年01月23日
    瀏覽(19)
  • 商城小程序(7.加入購物車)

    商城小程序(7.加入購物車)

    新建store目錄,并創(chuàng)建store.js文件用于管理vuex相關(guān)內(nèi)容 在store.js中 初始化store的實例對象 在main.js 中導入store實例對象并掛載到vue的實例上 同樣目錄下創(chuàng)建cart.js模塊 在cart.js中,初始化如下的vuex模塊 在store/store.js 模塊中,導入并掛載購物車中的vue模塊 商品詳情頁面測試,是

    2024年02月02日
    瀏覽(22)
  • 模擬瑞幸小程序購物車

    模擬瑞幸小程序購物車

    是根據(jù)渡一袁老師的大師課寫的,如有什么地方存在問題,還請大家指出來喲ど?????????う?~

    2024年01月16日
    瀏覽(22)
  • flask 與小程序 購物車刪除和編輯庫存功能

    編輯? :? ?數(shù)量加減? ?價格匯總? 數(shù)據(jù)清空 知識點1: view class=\\\"jian-btn\\\" catchtap=\\\"jianBtnTap\\\" data-index=\\\"{{index}}\\\" 是一個微信小程序中的一個視圖組件,具有以下特點: class=\\\"jian-btn\\\" :該組件的樣式類名為 jian-btn ,可以通過CSS樣式表對其進行樣式設置。 catchtap=\\\"jianBtnTap\\\" :該組件綁

    2024年01月22日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包