• 社区团购美团和多多买菜小程序购物车


    概述

    微信小程序购物车列表demo

    详细

    需求

    • 显示食物名称、价格、数量。

    • 点击相应商品增加按钮,购买数量增加1,点击食物减少按钮,购买数量减一

    • 显示购买总数和总金额

    • 查看当前购买的商品

    效果图(数据来自本地模拟)

    微信截图_20190314191639.png

    微信截图_20190314191623.png

    目录结构

    微信截图_20190315173105.png

    实现过程

        主要wxml

    1. <view class='foods'>
    2. <view class='left-food-menu'>
    3. <scroll-view class='scroll-view' scroll-y="true" style='height:{{screenHeight-150}}px'>
    4. <!-- 渲染类别名称 -->
    5. <view wx:key="id" wx:for="{{AllFoodList}}" data-index='{{index}}' bindtap='changeTypeSelected' class="{{nowFoodTypeSelectIndex==index ? 'food-type-title food-type-title-selected ' : 'food-type-title'}}">
    6. <view wx:if='{{nowFoodTypeSelectIndex==index}}' class='line'></view>
    7. <text >{{item.foodTitle}}</text>
    8. </view>
    9. </scroll-view>
    10. </view>
    11. <!-- 渲染食物列表 -->
    12. <view wx:key="id" wx:for="{{AllFoodList}}" wx:for-index="idx" data-index='{{idx}}' hidden='{{nowFoodTypeSelectIndex==idx?false:true}}' class='food-list'>
    13. <scroll-view class="scroll" scroll-y="true">
    14. <!-- 渲染这个分类下所有食物 -->
    15. <view wx:key="id" wx:for="{{item.foodList}}" wx:for-index="foodindex">
    16. <view class="cart_container">
    17. <image mode='aspectFill' class="item-image" src="{{item.goodPerview}}"></image>
    18. <view class="column">
    19. <view class='food-info'>
    20. <text class="food-title">{{item.goodName}}</text>
    21. <text class="colmun-margin sales">月销: {{item.goodSales}} 单</text>
    22. <view class="colmun-margin row">
    23. <view>
    24. <text class="sku-price"></text>
    25. <text class="sku-price">{{item.goodPrice}}</text>
    26. </view>
    27. <view class="{{item.buyNum>0?'add_border':'food-item-add'}}">
    28. <image bindtap='funFoodReduce' data-type_index='{{idx}}' data-food_index="{{foodindex}}" hidden='{{item.buyNum>0?false:true}}' class='img food-subtract' src='/imgs/ic_food_subtract.png'></image>
    29. <text hidden='{{item.buyNum>0?false:true}}' class='food-item-number'>{{item.buyNum}}</text>
    30. <image bindtap='funFoodAdd' data-type_index='{{idx}}' data-food_index="{{foodindex}}" class='img food-addto' src='/imgs/ic_food_item_add.png'></image>
    31. </view>
    32. </view>
    33. </view>
    34. </view>
    35. </view>
    36. <view class="separate"></view>
    37. </view>
    38. </scroll-view>
    39. </view>
    40. </view>

    点击商品增加按钮

    1. funFoodAdd: function (e) {
    2. this.calculationMoney;
    3. var foodIndex = e.currentTarget.dataset.food_index
    4. var typeIndex = e.currentTarget.dataset.type_index
    5. var nowNum = this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum
    6. nowNum = nowNum + 1
    7. var tempBuyTotal = this.data.buyTotal;
    8. this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum = nowNum
    9. this.setData({
    10. AllFoodList: this.data.AllFoodList, buyTotal: tempBuyTotal + 1
    11. })
    12. this.calculationMoney();
    13. },

    点击商品减少按钮

    1. //减去食物
    2. funFoodReduce: function (e) {
    3. var foodIndex = e.currentTarget.dataset.food_index
    4. var typeIndex = e.currentTarget.dataset.type_index
    5. var nowNum = this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum
    6. if (nowNum == 0) {
    7. return;
    8. }
    9. nowNum = nowNum - 1
    10. var tempBuyTotal = this.data.buyTotal;
    11. this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum = nowNum
    12. this.setData({
    13. AllFoodList: this.data.AllFoodList, buyTotal: tempBuyTotal - 1
    14. })
    15. this.calculationMoney();
    16. },

    计算购买商品价格

    1. //计算总价
    2. calculationMoney: function () {
    3. var tempMoney = 0;
    4. var parent = this.data.AllFoodList.length;
    5. for (var i = 0; i < parent; i++) {
    6. for (var j = 0; j < this.data.AllFoodList[i].foodList.length; j++) {
    7. tempMoney = tempMoney + (this.data.AllFoodList[i].foodList[j].buyNum * this.data.AllFoodList[i].foodList[j].goodPrice)
    8. }
    9. }
    10. this.setData({
    11. totalMoney: tempMoney
    12. })
    13. },
    14. //显示购物车
    15. showCartDialog: function () {
    16. this.setData({
    17. cartVisible: !this.data.cartVisible
    18. })
    19. },
    20. //清空购物车
    21. funCartEmpty: function () {
    22. var temp = this.data.AllFoodList;
    23. for (var i = 0; i < temp.length; i++) {
    24. for (var j = 0; j < temp[i].foodList.length; j++) {
    25. temp[i].foodList[j].buyNum = 0;
    26. }
    27. }
    28. this.calculationMoney();
    29. this.setData({
    30. AllFoodList: temp, buyTotal: 0
    31. })
    32. },

  • 相关阅读:
    JS手写章节(1)—手写实现call、apply、bind
    深度学习项目部署遇到的错误【记录】
    鸿蒙ArkTs开发问题总结
    Docker容器实现MySQL主从复制
    基于SSM框架流浪猫救援网站的设计与实现毕业设计源码201502
    iOS开发实战-仿小红书App开发-1-App创建与Git
    「Java开发指南」如何在Spring中使用JAX-WS注释器?
    零代码编程:用ChatGPT将特定文件标题重命名为特定格式
    Flask模板_循环结构
    【栈】224. 基本计算器
  • 原文地址:https://blog.csdn.net/hanjiepo/article/details/133253792