哈喽,大家好,快半个月没写了,现在提笔都有点生硬了,一直没更新的原因,一个是代码君也要上班,加上最近工作比较忙,还有就是写文章一直未被认可,所以没什么动力再创作了,那时真的坚持不下去,打算放弃了,感谢读者曹明,一个韩国的研究生读者,支持我,并给我鼓励,期待我更新下一篇,我非常感动,瞬间战斗力恢复,其实你们简单的点赞,评论,都是给我最大的支持,好了,煽情完毕,该讲今天的重点了,购物车,购物车的界面实现到不是很难,难点是处理里面的逻辑,无论是小程序,还是APP,购物车的逻辑都是最难的,下面开始教大家如何实现购物车了,先上效果图

##购物车实现
####1. cart.wxml

<import src="/template/quantity/index.wxml" />
<scroll-view class="scroll" scroll-y="true"><view class="separate"></view><view wx:for="{{carts}}"><view class="cart_container"><image class="item-select" bindtap="switchSelect" data-index="{{index}}" data-id="{{index}}" src="{{item.isSelect?'../../images/cart/comment_select.png':'../../images/cart/comment_normal.png'}}" /><image class="item-image" src="{{item.pic}}"></image><view class="column"><text class="title">{{item.name}}</text><view class="row"><text class="sku-price">¥</text><text class="sku-price">{{item.price}}</text><view class="sku"><template is="quantity" data="{{ ...item.count, componentId: index }}" /></view></view></view></view><view class="separate"></view></view>
</scroll-view>
<view class="bottom_total"><view class="bottom_line"></view><view class="row"><image class="item-allselect" bindtap="allSelect" src="{{isAllSelect?'../../images/cart/comment_select.png':'../../images/cart/comment_normal.png'}}" /><text class="small_text">全选</text><text>合计:¥ </text><text class="price">{{totalMoney}}</text><button class="button-red" bindtap="toBuy" formType="submit">去结算</button></view>
</view>

布局不是很复杂,一个循环列表,循环出购物车商品,外加一个结算的底部控件,还需要提醒的是,循环列表外面要加一层scroll-view,这样当数据很多是时候,可以滚动,不熟悉scroll-view的,请自行翻看前面几篇文章,里面有讲解

####2. cat.wxss

/* pages/cart/cart.wxss */
.cart_container {display: flex;flex-direction: row;
}
.scroll {margin-bottom: 120rpx;
}
.column {display: flex;flex-direction: column;
}
.row {display: flex;flex-direction: row;align-items: center;
}
.sku {margin-top: 60rpx;margin-left: 100rpx;
}
.sku-price {color: red;position: relative;margin-top: 70rpx;
}
.price {color: red;position: relative;
}
.title {font-size: 38rpx;margin-top: 40rpx;
}
.small_text {font-size: 28rpx;margin-right: 40rpx;margin-left: 10rpx;
}
.item-select {width: 40rpx;height: 40rpx;margin-top: 90rpx;margin-left: 20rpx;
}
.item-allselect {width: 40rpx;height: 40rpx;margin-left: 20rpx;
}
.item-image {width: 180rpx;height: 180rpx;margin: 20rpx;
}
.bottom_line {width: 100%;height: 2rpx;background: lightgray;
}
.bottom_total {position: fixed;display: flex;flex-direction: column;bottom: 0;width: 100%;height: 120rpx;line-height: 120rpx;background: white;
}
.button-red {background-color: #f44336; /* 红色 */
}
button {position: fixed;right: 0;color: white;text-align: center;display: inline-block;font-size: 30rpx;border-radius: 0rpx;width: 30%;height: 120rpx;line-height: 120rpx;
}

wxss样式没什么可说的,了解其属性,调用class就好,重点说一下cart.js,全篇的逻辑都在这里面

####3. cart.js

// pages/cart/cart.js
var Temp = require('../../template/contract.js');
Page(Object.assign({}, Temp.Quantity, {data: {isAllSelect:false,totalMoney:0,// 商品详情介绍carts: [{pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148058328876.jpg",name:"日本资生堂洗颜",price:200,isSelect:false,// 数据设定count: {quantity: 2,min: 1,max: 20},},{pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058301941.jpg',name: "倩碧焕妍活力精华露",price: 340,isSelect: false,// 数据设定count: {quantity: 1,min: 1,max: 20},},{pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/14805828016.jpg',name: "特效润肤露",price: 390,isSelect: false,// 数据设定count: {quantity: 3,min: 1,max: 20},},{pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058228431.jpg',name: "倩碧水嫩保湿精华面霜",price: 490,isSelect: false,// 数据设定count: {quantity: 1,min: 1,max: 20},},{pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148057953326.jpg',name: "兰蔻清莹柔肤爽肤水",price: 289,isSelect: false,// 数据设定count: {quantity: 10,min: 1,max: 20},},{pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148057921620_middle.jpg",name: "LANCOME兰蔻小黑瓶精华",price: 230,isSelect: false,// 数据设定count: {quantity: 1,min: 1,max: 20},},],},//勾选事件处理函数  switchSelect: function (e) {// 获取item项的id,和数组的下标值  var Allprice = 0,i=0;let id = e.target.dataset.id,index = parseInt(e.target.dataset.index);this.data.carts[index].isSelect = !this.data.carts[index].isSelect;//价钱统计if (this.data.carts[index].isSelect) {this.data.totalMoney = this.data.totalMoney + this.data.carts[index].price;}else {this.data.totalMoney = this.data.totalMoney - this.data.carts[index].price;}//是否全选判断for (i = 0; i < this.data.carts.length; i++) {Allprice = Allprice + this.data.carts[i].price;}if (Allprice == this.data.totalMoney){this.data.isAllSelect=true;}else {this.data.isAllSelect = false;}this.setData({carts: this.data.carts,totalMoney: this.data.totalMoney,isAllSelect: this.data.isAllSelect,})},//全选allSelect: function (e) {//处理全选逻辑let i = 0;if (!this.data.isAllSelect){for (i = 0; i < this.data.carts.length; i++) {this.data.carts[i].isSelect = true;this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price;}}else{for (i = 0; i < this.data.carts.length; i++) {this.data.carts[i].isSelect = false;}this.data.totalMoney=0;}this.setData({carts: this.data.carts,isAllSelect: !this.data.isAllSelect,totalMoney: this.data.totalMoney,})},// 去结算toBuy() {wx.showToast({title: '去结算',icon: 'success',duration: 3000});this.setData({showDialog: !this.data.showDialog});},//数量变化处理handleQuantityChange(e) {var componentId = e.componentId;var quantity = e.quantity;this.data.carts[componentId].count.quantity = quantity;this.setData({carts: this.data.carts,});}
}));

介绍一下用到的参数

  1. isAllSelect:是否全选
  2. totalMoney:总金额
  3. carts :购物车商品数据

switchSelect 勾选按钮需要做的逻辑处理

  1. 判断是否达到全部勾选,如果全部勾选,底部的全选按钮要点亮,判断依据是,价钱是否等于总价,当然这只是一种判断方式,读者也可以通过勾选的数量判断,
  2. 对勾选或取消的按钮,进行总价的加减法计算
  3. this.setData,更新数据,这个是重点,每次处理完数据,都要记得更新数据

allSelect 全选按钮的逻辑处理

  1. 全选就把每个item勾选图标点亮,然后统计总价钱,不全选就置为灰色,总价钱为0
  2. this.setData更新数据

##微信小程序数据处理

####一、修改数据方式

data:{name:'我是初始化的name'
}

#####1. this.data.name

 this.data.name='我是代码君data'

#####2. this.setData

 this.setData({name:'我是代码君setData'})

这两种方式都可以改变数据,this.setData的好处是可以有刷新的效果,即实时更新数据

####二、修改对象数组

data:{
person:{name:'代码君',city:'厦门'
}
}

#####1. 修改全部对象

this.setData({person:{name:'新代码君',city:'湖南'}})

#####2. 修改部分数据

this.setData({'person.name': '代码君只修改名字'})
//多个数组用这个
this.setData({'person[0].name': '代码君只修改名字'})

####三、添加删除数据
#####1. 添加数据concat

//假设这一段是我们要新增的数组
var newarray = [{name:'增加的数据--'+new Date().getTime() ,
}];
//向前--用newarray与this.data.list合拼
this.data.list = newarray.concat(this.data.list);
//向后--用this.data.list与newarray合拼
this.data.list = this.data.list.concat(newarray);

#####2. 删除数据**splice()**删除数据,然后返回被删除的数据

//删除remove:function (e){var dataset = e.target.dataset;var Index = dataset.index;//通过index识别要删除第几条数据,第二个数据为要删除的项目数量,通常为1this.data.list.splice(Index,1);//渲染数据this.setData({list:this.data.list});}

#####3. 清空数据

//清空clear:function (){//其实就是让数组变成一个空数组即可this.setData({list:{}});}

##总结

今天主要讲解js是如何处理数据逻辑的,也讲解了数据的增删改查,这是必备的知识项,回去要多多练习。好了今天就讲到这,祝大家周末愉快~

###上一篇:微信小程序实战篇-商品详情页(二)

推荐一本好书,适合新手小白

书名:【小程序从0到1:微信全栈工程师一本通】 下载链接

##关注微信公众号获取最新资讯

微信小程序实战篇-购物车相关推荐

  1. 微信小程序实战篇:实现抖音评论效果

    IT实战联盟博客:http://blog.100boot.cn 我们在写小程序的时候经常会遇到弹出层的效果而现有官网提供的跳转方法多数是不支持参数传递的.本文教大家做一个抖音评论效果的小程序 首先看下 ...

  2. 微信小程序实战篇-商品详情页(二)

    今天要讲解商品详情页中sku的弹出选着框,这个涉及css动画样式,css动画是新的知识点,我们之前并没有接触过,请大家做好笔记,我们要做的效果是酱紫的~ 这个布局难点是需要绘制一个阴影背景.弹出的动画 ...

  3. 微信小程序实战篇:商品属性联动选择(案例)

    本期的微信小程序实战篇来做一个电商网站经常用到的-商品属性联动选择的效果,素材参考了一点点奶茶. 效果演示: 商品属性联动.gif 代码示例 1.commodity.xml <!-- <v ...

  4. 微信小程序实战篇-下拉刷新与加载更多

    下拉刷新 实现下拉刷新目前能想到的有两种方式 调用系统的API,系统有提供下拉刷新的API接口 下拉刷新API.png 监听scroll-view,自定义下拉刷新,还记得scroll-view里面有一 ...

  5. 微信小程序实战篇-电商(一)

    哈喽,大家好,端午节过的怎么样啊,代码君可是没休息,专心的写电商文章哦,也是蛮拼的,如果对代码君认可的话,欢迎点赞转发,你们的点赞转发是对我最大的支持!好啦,言归正传,我们今天要讲解微信小程序的实战篇 ...

  6. 前端微信小程序实战篇

    电商底部导航栏的制作 我想大家对电商一定不陌生,一般电商的底部导航栏有以下几个首页.分类.购物车.个人中心. app.json是用来配置page路径以及导航栏属性的,那我们要做首页.分类.购物车.个人 ...

  7. python爬取微信小程序(实战篇)_爬虫爬取微信小程序

    之前打算做个微信小程序的社区,所以写了爬虫去爬取微信小程序,后面发现做微信小程序没有前途,就把原来的项目废弃了做了现在的网站观点,不过代码放着也是放着,还不如公开让大家用,所以我把代码贴出来,有需要的 ...

  8. python爬取微信小程序(实战篇)_python爬取猫眼的前100榜单并展示在微信小程序

    首先分析要爬取的网页,对其结构及数据获取方式解析后,可采用正则筛选自己要的数据 猫眼榜单.png import requests from requests.exceptions import Req ...

  9. 微信小程序实战篇-商品详情页(一)

    哈喽,大家好,今天要进入新篇章啦,商品详情页,这个可是个大模块,要分好几次才能讲解清楚,现在我们先进行第一讲,老规矩,先上效果图 有木有很酷炫啊,下面由代码君教你如何实现. 详情页布局 看效果图,可以 ...

最新文章

  1. mac系统下,解决git clone速度慢导至失败的问题
  2. let/var——事实上var的设计可以看成JavaScript语言设计上的错误. 但是这种错误多半不能修复和移除, 以为需要向后兼容.||将let看成更完美的var
  3. 如何与人交流——程序员,赶紧生个孩子吧!
  4. 6-1 数组元素的区间删除
  5. python的datetime函数_Python连载8-datetime包函数介绍
  6. 【数据结构与算法】森林版并查集V1.0的Java实现
  7. 东山再起?这将是锤子新手机?或搭配全键盘...
  8. SpringCloud和SpringBoot组件对比
  9. CentOS 7 多网卡绑定
  10. python画三维图-Python使用matplotlib绘制三维图形示例
  11. 百分点感知智能实验室:语音识别技术发展阶段探究
  12. Ae 入门系列之十二:形状动画
  13. leetcode No5. Longest Palindromic Substring
  14. Spring Boot将端口8080重定向到8443
  15. 415 http请求 hutool_HTTP状态码(响应码)之客户端错误400
  16. php 倒计时抢购,限时抢购-倒计时_HTML/CSS_互联网开发技术网_传播最新的编程技术_php361.com...
  17. matlab pcode 单步运行,[转载]Matlab中的profile工具
  18. ww:action用法和给ww:select赋值
  19. 史上最简SLAM零基础解读(10.1) - g2o(图优化)→简介环境搭建(slam十四讲第二版为例)
  20. Jmeter(1)----下载jmeter并运行一个简单用例

热门文章

  1. 脚本实现自动抢红包(仅供学习交流)
  2. SpringBoot--启动时的事件机制
  3. 十大排序算法:快速排序算法
  4. 阿里巴巴java开发编码规范——单元测试
  5. STM32F103ZE的定时器
  6. web前端应届生面试注意事项
  7. mastering mysql_Mastering The Faster Web with PHP, MySQL, and JavaScript
  8. 为什么中国商标在海外被频繁抢注
  9. 腾讯云服务器怎么样?性能及安全性测评
  10. Unity WebGL开发