购物车底部工具栏全选、总价、总数量实现,购物车商品复选框中业务处理

<!-- 收货地址开始  -->
<view class="receive_address_row">
    <view class="address_btn" wx:if="{{!address}}">
        <button type="warn" plain bindtap="handleChooseAddress">获取收货地址</button>
    </view>

    <view wx:else  class="user_info_row"> 
        <view class="user_info">
         <view>收货人:{{address.userName}},{{address.telNumber}}</view>
         <view>{{address.provinceName+address.cityName+address.countyName+address.detailInfo}}</view>
        </view>

        <view class="change_address_btn">
            <button size="mini" plain>更换地址</button>
        </view>
    </view>
</view>
<!-- 收货地址结束  -->

<!-- 购物车清单 开始 -->
<view class="cart_content">
    <view class="cart_main">
        <view class="cart_item" wx:for="{{cart}}" wx:key="id">
            <!-- 复选框 开始 -->
            <view class="cart_chk_wrap">
                <checkbox-group data-id="{{item.id}}" bindchange="handleItemChange">
                    <checkbox checked="{{item.checked}}"></checkbox>
                </checkbox-group>
            </view>
            <!-- 复选框 结束 -->

            <!-- 商品图片 开始 -->
            <navigator class="cart_img_wrap"  url="/pages/product_detail/index?id={{item.id}}">
                <image src="{{baseUrl+'/image/product/'+item.proPic}}" mode="widthFix"></image>
            </navigator>
            <!-- 商品图片 结束 -->

            <!-- 商品信息 开始 -->
            <view class="cart_info_wrap">
                <navigator url="/pages/product_detail/index?id={{item.id}}">
                    <view class="goods_name">{{item.name}}</view>
                </navigator>
                <view class="goods_price_wrap">
                    <view class="goods_price">{{item.price}}
                    </view>
                    <view class="cart_num_tool">
                        <view class="num_edit">-</view>
                        <view class="goods_num">{{item.num}}</view>
                        <view class="num_edit">+</view>
                    </view>
                </view>
            </view>
            <!-- 商品信息 结束 -->
        </view>
    </view>
</view>
<!-- 购物车清单 结束 -->

<!-- 底部工具栏 开始 -->
<view class="footer_tool">
    <!-- 全选 开始 -->
    <view class="all_chk_wrap">
        <checkbox-group>
            <checkbox checked="{{allChecked}}"><text decode="true">&nbsp;&nbsp;全选</text></checkbox>
        </checkbox-group>
    </view>
    <!-- 全选 结束 -->

    <!-- 合计 开始 -->
            <view class="total_price_wrap">
                <view class="total_price">
                    合计: <text class="total_price_text">{{totalPrice}}</text>
                </view>
            </view>
    <!-- 合计 结束 -->

    <!-- 结算 开始 --> 
    <view class="order_pay_wrap">
        结算({{totalNum}})
    </view>
    <!-- 结算 结束 -->

</view>
<!-- 底部工具栏 结束 -->

//导入request请求工具类
import {getBaseUrl,requestUtil} from '../../utils/requestUtil';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    baseUrl:'',
    cart:[],
    allChecked:false,
    totalPrice:0,
    totalNum:0
  },

  handleChooseAddress(){
    wx.chooseAddress({
        success:(result)=>{
            console.log(result)
            wx.setStorageSync('address', result)
        },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const baseUrl = getBaseUrl();
    this.setData({
        // swiperList:result.message,
        baseUrl
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log("show");
    const address=wx.getStorageSync('address');
    const cart=wx.getStorageSync('cart')||[];
    
    this.setData({
        address
    })

    this.setCart(cart);
  },

  //商品选中事件处理
  handleItemChange(e){
        const {id}=e.currentTarget.dataset;
        let {cart}=this.data;
        let index=cart.findIndex(v=>v.id===id); 
        console.log(index)
        cart[index].checked=!cart[index].checked;

        this.setCart(cart);
  },

  //设置购物车状态 重新计算 底部工具栏 全选 总价  总数量 重新设置缓存
  setCart(cart){
    let allChecked=true;
    let totalPrice=0;
    let totalNum=0;
    cart.forEach(v=>{
        if(v.checked){
            totalPrice+=v.price*v.num;
            totalNum+=v.num;
        }else{
            allChecked=false;
        }
    })
    allChecked=cart.length!=0?allChecked:false;
    this.setData({
        cart,
        allChecked,
        totalNum,
        totalPrice
    })

    //cart设置到缓存中
    wx.setStorageSync('cart', cart);
  }
})
page{
    padding-bottom: 70rpx;
}

.receive_address_row{
    .address_btn{
        padding: 20rpx;
        button{
            width: 60%;
        }
    }

    .user_info_row{
        display: flex;
        padding: 20rpx  ;
        .user_info{
            flex: 5;
        }
        .change_address_btn{
            flex: 3;
            text-align: right;
            button{
                border: 1px solid gray;
                font-weight: normal; 
            }
        }
    }
}


.cart_content{
    background-color: #f5f5f5;
    .cart_main{
        padding: 2rpx 10rpx 10rpx 10rpx;
        .cart_item{
            display: flex;
            background-color: white;
            border-radius: 10px;
            margin: 20rpx;
            padding-right: 20rpx;
            .cart_chk_wrap{
                flex: 1;
                display: flex;
                justify-content: center;
                align-items: center;
                margin: 20rpx;
            }
            .cart_img_wrap{
                flex: 2;
                display: flex;
                justify-content: center;
                align-items: center;
                background-color: #f5f5f5;
                margin: 20rpx;
                border-radius: 10px;
                image{
                    width: 80%;
                }
            }

            .cart_info_wrap{
                flex: 4 ;
                display: flex;
                flex-direction: column;
                justify-content: space-around;
                .goods_name{
                    font-weight: bolder;
                    display: -webkit-box;
                    overflow: hidden;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;
                }
                .goods_price_wrap{
                    display: flex;
                    justify-content: space-between;
                     .goods_price{
                        color: var(--themeColor);
                        font-size: 34rpx;
                     }
                     .cart_num_tool{
                         display: flex;
                         .num_edit{
                            display: flex;
                            justify-content: center;
                            align-items: center;
                            width: 55rpx;
                            height: 55rpx;
                            font-weight: bolder;
                         }
                         .goods_num{
                            display: flex;
                            justify-content: center;
                            align-items: center;
                            width: 85rpx;
                            height: 55rpx;
                            background-color: #f5f5f5;
                         }
                     }
                }
            }
        }
    }
}

.footer_tool{
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 90rpx;
    background-color: #fff;
    border-top: 1px solid #ccc;
    .all_chk_wrap{
        flex: 2;
         display: flex;
         justify-content: center;
         align-items: center;
         padding-left: 25rpx;
    }
    .total_price_wrap{
        flex: 5;
        display: flex;
        justify-content: center;
        align-items: center;
        .total_price{
            .total_price_text{
                color: var(--themeColor);
                font-weight: bolder;
                font-size: 34rpx;
            }
        }
    }

    .order_pay_wrap{
        flex: 3;
        display: flex;
        justify-content: center;
        align-items: center;
        background-image: linear-gradient(90deg,#ff7408,#fe6227);
        color: #fff;
        font-size: 32rpx;
        font-weight: 600;
        border-radius: 20px;
        margin: 10rpx   ;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript和HTML,创建一个购物车复选框全选/单选功能可以通过以下几个步骤完成。这里是一个基本的示例: ```html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>购物车复选框</title> <style> .cart-checks { display: flex; gap: 10px; /* 控制复选框之间的间距 */ } .check-all { margin-right: 10px; } </style> </head> <body> <h2>购物车商品选择</h2> <div class="cart-checks"> <!-- 商品项,这里的数量可以动态添加 --> <label class="checkbox" for="item1"> <input type="checkbox" id="item1" value="商品1"> 商品1 </label> <label class="checkbox" for="item2"> <input type="checkbox" id="item2" value="商品2"> 商品2 </label> <!-- 更多商品... --> <button id="checkAll">全选</button> <button id="uncheckAll">全不选</button> </div> <script> // 获取所有复选框全选按钮 const checkboxes = document.querySelectorAll('input[type="checkbox"]'); const checkAllBtn = document.getElementById('checkAll'); const uncheckAllBtn = document.getElementById('uncheckAll'); // 全选按钮事件处理 checkAllBtn.addEventListener('click', function() { checkboxes.forEach(function(checkbox) { checkbox.checked = true; }); }); // 全不选按钮事件处理 uncheckAllBtn.addEventListener('click', function() { checkboxes.forEach(function(checkbox) { checkbox.checked = false; }); }); // 当复选框状态改变时,检查是否有被选的 checkboxes.forEach(function(checkbox) { checkbox.addEventListener('change', function() { if (checkboxes.filter(checkbox => checkbox.checked).length === checkboxes.length) { checkAllBtn.textContent = '全选'; } else { checkAllBtn.textContent = '全不选'; } }); }); </script> </body> </html> ``` 这个示例展示了如何使用JavaScript控制复选框全选和全不选操作,并且当任何一个复选框的状态变化时,会自动更新全选按钮的状态。你可以根据实际需求对商品列表和样式进行扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值