<_小程序模拟淘宝购物车">
当前位置:   article > 正文

微信小程序模仿购物车页面_小程序模拟淘宝购物车

小程序模拟淘宝购物车

本篇抽取demo部分功能展示,在文章结尾附上完整DEMO

微信小程序其它功能介绍

一、效果图:

二、.WXML代码示例,

  1. <view class="main">
  2. <view wx:if="{{hasList}}">
  3. <view class="cart-box">
  4. <view class="cart-list" wx:for="{{carts}}" wx:key="{{index}}">
  5. <icon wx:if="{{item.selected}}" type="success" color="red" data-index="{{index}}" class="cart-pro-select" bindtap="selectList"/>
  6. <icon wx:else type="circle" class="cart-pro-select" data-index="{{index}}" bindtap="selectList"/>
  7. <navigator url="../details/details?id={{item.id}}"><image class="cart-thumb" src="{{item.image}}"></image></navigator>
  8. <text class="cart-pro-name">{{item.title}}</text>
  9. <text class="cart-pro-price">¥{{item.price}}</text>
  10. <view class="cart-count-box">
  11. <text class="cart-count-down" bindtap="minusCount" data-obj="{{obj}}" data-index="{{index}}">-</text>
  12. <text class="cart-count-num">{{item.num}}</text>
  13. <text class="cart-count-add" bindtap="addCount" data-index="{{index}}">+</text>
  14. </view>
  15. <text class="cart-del" bindtap="deleteList" data-index="{{index}}">×</text>
  16. </view>
  17. </view>
  18. <view class="cart-footer">
  19. <icon wx:if="{{selectAllStatus}}" type="success_circle" color="#fff" class="total-select" bindtap="selectAll"/>
  20. <icon wx:else type="circle" color="#fff" class="total-select" bindtap="selectAll"/>
  21. <view class="order-icon">
  22. <navigator url="../orders/orders"><image src="/image/icon3.png"></image></navigator>
  23. </view>
  24. <text>全选</text>
  25. <text class="cart-toatl-price">¥{{totalPrice}}</text>
  26. </view>
  27. </view>
  28. <view wx:else>
  29. <view class="cart-no-data">购物车是空的哦~</view>
  30. </view>
  31. </view>

二、.JS代码示例

  1. Page({
  2. data: {
  3. carts: [], // 购物车列表
  4. hasList: false, // 列表是否有数据
  5. totalPrice: 0, // 总价,初始为0
  6. selectAllStatus: true, // 全选状态,默认全选
  7. obj: {
  8. name: "hello"
  9. }
  10. },
  11. onShow() {
  12. this.setData({
  13. hasList: true,
  14. carts: [ //模拟数据
  15. {
  16. id: 1,
  17. title: '测试土豆 1斤',
  18. image: '/pages/images/s5.png',
  19. num: 4,
  20. price: 10.00,
  21. selected: true
  22. },
  23. {
  24. id: 2,
  25. title: '测试黑米 1斤',
  26. image: '/pages/images/s6.png',
  27. num: 1,
  28. price: 5.0,
  29. selected: true
  30. }
  31. ]
  32. });
  33. this.getTotalPrice();
  34. },
  35. /**
  36. * 当前商品选中事件
  37. */
  38. selectList(e) {
  39. const index = e.currentTarget.dataset.index;
  40. let carts = this.data.carts;
  41. const selected = carts[index].selected;
  42. carts[index].selected = !selected;
  43. this.setData({
  44. carts: carts
  45. });
  46. this.getTotalPrice();
  47. },
  48. /**
  49. * 删除购物车当前商品
  50. */
  51. deleteList(e) {
  52. const index = e.currentTarget.dataset.index;
  53. let carts = this.data.carts;
  54. carts.splice(index, 1);
  55. this.setData({
  56. carts: carts
  57. });
  58. if (!carts.length) {
  59. this.setData({
  60. hasList: false
  61. });
  62. } else {
  63. this.getTotalPrice();
  64. }
  65. },
  66. /**
  67. * 购物车全选事件
  68. */
  69. selectAll(e) {
  70. let selectAllStatus = this.data.selectAllStatus;
  71. selectAllStatus = !selectAllStatus;
  72. let carts = this.data.carts;
  73. for (let i = 0; i < carts.length; i++) {
  74. carts[i].selected = selectAllStatus;
  75. }
  76. this.setData({
  77. selectAllStatus: selectAllStatus,
  78. carts: carts
  79. });
  80. this.getTotalPrice();
  81. },
  82. /**
  83. * 绑定加数量事件
  84. */
  85. addCount(e) {
  86. const index = e.currentTarget.dataset.index;
  87. let carts = this.data.carts;
  88. let num = carts[index].num;
  89. num = num + 1;
  90. carts[index].num = num;
  91. this.setData({
  92. carts: carts
  93. });
  94. this.getTotalPrice();
  95. },
  96. /**
  97. * 绑定减数量事件
  98. */
  99. minusCount(e) {
  100. const index = e.currentTarget.dataset.index;
  101. const obj = e.currentTarget.dataset.obj;
  102. let carts = this.data.carts;
  103. let num = carts[index].num;
  104. if (num <= 1) {
  105. return false;
  106. }
  107. num = num - 1;
  108. carts[index].num = num;
  109. this.setData({
  110. carts: carts
  111. });
  112. this.getTotalPrice();
  113. },
  114. /**
  115. * 计算总价
  116. */
  117. getTotalPrice() {
  118. let carts = this.data.carts; // 获取购物车列表
  119. let total = 0;
  120. for (let i = 0; i < carts.length; i++) { // 循环列表得到每个数据
  121. if (carts[i].selected) { // 判断选中才会计算价格
  122. total += carts[i].num * carts[i].price; // 所有价格加起来
  123. }
  124. }
  125. this.setData({ // 最后赋值到data中渲染到页面
  126. carts: carts,
  127. totalPrice: total.toFixed(2)
  128. });
  129. }
  130. })

三、.XWSS代码示例

  1. .cart-box{
  2. padding-bottom: 100rpx;
  3. }
  4. .cart-list{
  5. position: relative;
  6. padding: 20rpx 20rpx 20rpx 285rpx;
  7. height: 185rpx;
  8. border-bottom: 1rpx solid #e9e9e9;
  9. }
  10. .cart-list .cart-pro-select{
  11. position: absolute;
  12. left: 20rpx;
  13. top: 90rpx;
  14. width: 45rpx;
  15. height: 45rpx;
  16. }
  17. .cart-list .cart-thumb{
  18. position: absolute;
  19. top: 20rpx;
  20. left: 85rpx;
  21. width: 185rpx;
  22. height: 185rpx;
  23. }
  24. .cart-list .cart-pro-name{
  25. display: inline-block;
  26. width: 300rpx;
  27. height: 105rpx;
  28. line-height: 50rpx;
  29. overflow: hidden;
  30. }
  31. .cart-list .cart-pro-price{
  32. display: inline-block;
  33. float: right;
  34. height: 105rpx;
  35. line-height: 50rpx;
  36. }
  37. .cart-list .cart-count-box{
  38. position: absolute;
  39. left: 285;
  40. bottom: 20rpx;
  41. width: 250rpx;
  42. height: 80rpx;
  43. }
  44. .cart-list .cart-count-box text{
  45. display: inline-block;
  46. line-height: 80rpx;
  47. text-align: center;
  48. }
  49. .cart-count-down,.cart-count-add{
  50. font-size: 44rpx;
  51. width: 50rpx;
  52. height: 100%;
  53. }
  54. .cart-count-num{
  55. width: 150rpx;
  56. }
  57. .cart-del{
  58. position: absolute;
  59. right: 20rpx;
  60. bottom: 20rpx;
  61. width: 80rpx;
  62. height: 80rpx;
  63. line-height: 80rpx;
  64. text-align: center;
  65. font-size: 44rpx;
  66. }
  67. .cart-footer{
  68. position: fixed;
  69. bottom: 0;
  70. left: 0;
  71. width: 100%;
  72. height: 90rpx;
  73. line-height: 90rpx;
  74. padding:0 100rpx 0 80rpx;
  75. box-sizing: border-box;
  76. background: #AB956D;
  77. color: #fff;
  78. }
  79. .total-select{
  80. position: absolute;
  81. left: 20rpx;
  82. top: 25rpx;
  83. width: 45rpx;
  84. height: 45rpx;
  85. }
  86. .order-icon{
  87. position: absolute;
  88. right: 40rpx;
  89. top: 25rpx;
  90. width: 45rpx;
  91. height: 45rpx;
  92. }
  93. .order-icon image,.order-icon navigator{
  94. display: block;
  95. width: 45rpx;
  96. height: 45rpx;
  97. }
  98. .cart-toatl-price{
  99. float: right;
  100. width: 120rpx;
  101. }
  102. .cart-no-data{
  103. padding:40rpx 0;
  104. color: #999;
  105. text-align: center;
  106. }

 四、.JSON代码示例

  1. {
  2. "navigationBarTitleText": "购物车"
  3. }

   DEMO

                                                     -END

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/517166
推荐阅读
相关标签