Selaa lähdekoodia

购物车添加全选

xiaohaizhao 2 vuotta sitten
vanhempi
commit
33ca4ede80

+ 5 - 0
packageA/shopping/modules/base.scss

@@ -90,6 +90,10 @@
 	}
 }
 
+/* .icon-class {
+	margin-top: -80rpx !important;
+} */
+
 .footer {
 	width: 100vw;
 	min-height: 130rpx;
@@ -108,6 +112,7 @@
 		color: #333333;
 
 		.sum {
+			display: flex;
 			height: 40rpx;
 			line-height: 40rpx;
 			font-size: 28rpx;

+ 58 - 6
packageA/shopping/modules/standard/index.js

@@ -20,6 +20,7 @@ Component({
         sa_brandid: null, //当前选中品牌id
         classList: [], //生成订单时所选
         sum: 0, //价格合
+        isAll: false
     },
     methods: {
         /* 获取列表 */
@@ -41,14 +42,27 @@ Component({
                     title: res.msg,
                     icon: "none"
                 })
+                let list = [],
+                    allBrandList = [];
+                list = res.data.map(v => {
+                    v.showPrice = CNY(v.oldprice)
+                    let obj = allBrandList.find(s => s.sa_brandid == v.sa_brandid);
+                    if (obj) {
+                        obj.results.push(v.sa_shoppingcartid)
+                    } else {
+                        allBrandList.push({
+                            brandname: v.brandname,
+                            sa_brandid: v.sa_brandid,
+                            results: [v.sa_shoppingcartid],
+                        })
+                    }
+                    return v
+                });
                 this.setData({
-                    list: res.data.map(v => {
-                        v.showPrice = CNY(v.oldprice)
-                        return v
-                    }),
+                    list,
+                    allBrandList,
                     isGet: true
                 });
-
                 if (wx.getStorageSync('shopping')) {
                     this.setData({
                         ...wx.getStorageSync('shopping')
@@ -104,14 +118,44 @@ Component({
                 this.getList();
             })
         },
+        /* 是否选择全部 */
+        setIsAll() {
+            let isAll = this.data.isAll;
+            //取消全选
+            if (isAll) {
+                this.setData({
+                    sa_brandid: null,
+                    results: []
+                })
+            } else {
+                //已选品牌产品情况下
+                if (this.data.sa_brandid) {
+                    let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
+                    this.setData({
+                        results: obj.results
+                    })
+                } else {
+                    if (this.data.allBrandList.length == 0) return;
+                    this.setData({
+                        sa_brandid: this.data.allBrandList[0].sa_brandid,
+                        results: this.data.allBrandList[0].results
+                    })
+                }
+            };
+            this.computeSum();
+        },
         /* 切换选中项 */
         changeResults(e, my = false) {
             const {
                 item
             } = my ? e : e.currentTarget.dataset;
+
+
             let results = this.data.results,
                 sa_brandid = this.data.sa_brandid;
+
             if (sa_brandid && sa_brandid != item.sa_brandid) return;
+
             if (results.length == 0) {
                 results.push(item.sa_shoppingcartid);
                 sa_brandid = item.sa_brandid;
@@ -167,8 +211,14 @@ Component({
                 results,
                 sa_brandid
             })
+            let isAll = false;
+            if (sa_brandid) {
+                let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
+                isAll = brand.results.length == results.length
+            }
             this.setData({
                 sum: CNY(sum),
+                isAll,
                 results,
                 sa_brandid,
                 classList
@@ -208,8 +258,10 @@ Component({
                     title: res.msg,
                     icon: "none"
                 });
+                let index = this.data.allBrandList.findIndex(v => v.sa_brandid == item.sa_brandid)
                 this.setData({
-                    list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
+                    list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid),
+                    [`allBrandList[${index}].results`]: this.data.allBrandList[index].results.filter(v => v != item.sa_shoppingcartid)
                 })
                 if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
                     item

+ 1 - 1
packageA/shopping/modules/standard/index.wxml

@@ -34,7 +34,7 @@
 <view class="footer">
     <view class="left">
         <view class="sum">
-            商品合计:<text>{{sum}}</text>
+            <van-checkbox icon-class='icon-class' icon-size="26rpx" shape='square' value="{{ isAll }}" bind:change="setIsAll">全选 合计:</van-checkbox><text>{{sum}}</text>
         </view>
         <view class="transport">
             {{logistics}}

+ 56 - 10
packageA/shopping/modules/tool/index.js

@@ -40,14 +40,28 @@ Component({
                     title: res.msg,
                     icon: "none"
                 })
+
+                let list = [],
+                    allBrandList = [];
+                list = res.data.map(v => {
+                    v.showPrice = CNY(v.oldprice)
+                    let obj = allBrandList.find(s => s.sa_brandid == v.sa_brandid);
+                    if (obj) {
+                        obj.results.push(v.sa_shoppingcartid)
+                    } else {
+                        allBrandList.push({
+                            brandname: v.brandname,
+                            sa_brandid: v.sa_brandid,
+                            results: [v.sa_shoppingcartid],
+                        })
+                    }
+                    return v
+                });
                 this.setData({
-                    list: res.data.map(v => {
-                        v.showPrice = CNY(v.oldprice)
-                        return v
-                    }),
+                    list,
+                    allBrandList,
                     isGet: true
                 });
-
                 if (wx.getStorageSync('shopping_tool')) {
                     this.setData({
                         ...wx.getStorageSync('shopping_tool')
@@ -56,7 +70,6 @@ Component({
                 }
             })
         },
-
         clickBut(e) {
             this.data.classList.length >= 2 ? wx.showToast({
                 title: '请选择订单领域(订单只允许同品牌/同领域的商品)',
@@ -104,6 +117,32 @@ Component({
                 this.getList();
             })
         },
+        /* 是否选择全部 */
+        setIsAll() {
+            let isAll = this.data.isAll;
+            //取消全选
+            if (isAll) {
+                this.setData({
+                    sa_brandid: null,
+                    results: []
+                })
+            } else {
+                //已选品牌产品情况下
+                if (this.data.sa_brandid) {
+                    let obj = this.data.allBrandList.find(v => v.sa_brandid == this.data.sa_brandid)
+                    this.setData({
+                        results: obj.results
+                    })
+                } else {
+                    if (this.data.allBrandList.length == 0) return;
+                    this.setData({
+                        sa_brandid: this.data.allBrandList[0].sa_brandid,
+                        results: this.data.allBrandList[0].results
+                    })
+                }
+            };
+            this.computeSum();
+        },
         /* 切换选中项 */
         changeResults(e, my = false) {
             const {
@@ -167,8 +206,14 @@ Component({
                 results,
                 sa_brandid
             })
+            let isAll = false;
+            if (sa_brandid) {
+                let brand = this.data.allBrandList.find(v => v.sa_brandid == sa_brandid)
+                isAll = brand.results.length == results.length
+            }
             this.setData({
                 sum: CNY(sum),
+                isAll,
                 results,
                 sa_brandid,
                 classList
@@ -197,8 +242,7 @@ Component({
         deteleItem(e) {
             const {
                 item
-            } = e.currentTarget.dataset,
-                that = this;
+            } = e.currentTarget.dataset;
             _Http.basic({
                 "id": 20220924095202,
                 "content": {
@@ -209,10 +253,12 @@ Component({
                     title: res.msg,
                     icon: "none"
                 });
+                let index = this.data.allBrandList.findIndex(v => v.sa_brandid == item.sa_brandid)
                 this.setData({
-                    list: that.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid)
+                    list: this.data.list.filter(v => v.sa_shoppingcartid != item.sa_shoppingcartid),
+                    [`allBrandList[${index}].results`]: this.data.allBrandList[index].results.filter(v => v != item.sa_shoppingcartid)
                 })
-                if (that.data.results.some(v => v == item.sa_shoppingcartid)) that.changeResults({
+                if (this.data.results.some(v => v == item.sa_shoppingcartid)) this.changeResults({
                     item
                 }, true);
                 getApp().globalData.num = getApp().globalData.num - 1;

+ 4 - 4
packageA/shopping/modules/tool/index.wxml

@@ -35,7 +35,7 @@
 <view class="footer">
     <view class="left">
         <view class="sum">
-            工具合计:<text>{{sum}}</text>
+            <van-checkbox icon-class='icon-class' icon-size="26rpx" shape='square' value="{{ isAll }}" bind:change="setIsAll">全选 合计:</van-checkbox><text>{{sum}}</text>
         </view>
         <view class="transport">
             {{logistics}}
@@ -56,7 +56,7 @@
     }
 </wxs>
 <wxs module="gap">
-	module.exports.price = function (figure, defaultamount) {
-		return (figure - defaultamount).toFixed(2)
-	}
+    module.exports.price = function (figure, defaultamount) {
+        return (figure - defaultamount).toFixed(2)
+    }
 </wxs>