[关闭]
@2890594972 2018-04-12T08:05:03.000000Z 字数 3785 阅读 1042

面向对象封装购物车

面向对象 1511


定义大对象

  1. // 产品对象
  2. function Product(id, name, price, num) {
  3. this.id = id;
  4. this.name = name;
  5. this.price = price;
  6. this.num = num;
  7. }
  8. // 购物车对象
  9. function ShopCar() {
  10. //存储对象
  11. this.productArr = [];
  12. }

定义属性和方法

  1. // 产品对象
  2. function Product(id, name, price, num) {
  3. this.id = id;
  4. this.name = name;
  5. this.price = price;
  6. this.num = num;
  7. }
  8. // 购物车对象
  9. function ShopCar() {
  10. //存储对象
  11. this.productArr = [];
  12. }
  13. // 添加产品
  14. ShopCar.prototype.add = function () {}
  15. // 删除商品
  16. ShopCar.prototype.del = function () {}
  17. // 找产品
  18. ShopCar.prototype.findProduct = function (id) {}
  19. // 选中
  20. ShopCar.prototype.check = function () {}
  21. // 取消选中
  22. ShopCar.prototype.uncheck = function () {}
  23. // 添加num
  24. ShopCar.prototype.plus = function () {}
  25. // 减少num
  26. ShopCar.prototype.minus = function () {}

书写业务逻辑

实现选中功能逻辑

01.实例化购物车

  1. // 第一步:使用对象
  2. window.onload = function(){
  3. var shop = new ShopCar();
  4. }

02.实现添加商品

  1. // 添加产品
  2. ShopCar.prototype.add = function (p) {
  3. this.productArr.push(p);
  4. console.log("成功添加一个商品:",this.productArr);
  5. }

03.实现选中商品

  1. // 选中
  2. ShopCar.prototype.check = function (dom) {
  3. var shopInfo = $(dom).parents('.shop-info');
  4. // Product(id, name, price, num)
  5. var id = shopInfo.data('id');
  6. console.log(shopInfo.data('id'));
  7. var name = shopInfo.find('h4').text();
  8. var price = +shopInfo.find('.price').text();
  9. var num = +shopInfo.find('.num').text();
  10. var pro = new Product(id, name, price, num);
  11. // 保存到购物车
  12. this.add(pro);
  13. // 更新价格
  14. this.updateTotalPrice();
  15. }

04.实现更新价格

  1. // 更新价格
  2. ShopCar.prototype.updateTotalPrice = function(){
  3. var total = 0;
  4. for(var i=0; i< this.productArr.length; i++){
  5. total += this.productArr[i]['price']*this.productArr[i]['num'];
  6. }
  7. $("#AllTotal").text(total);
  8. }

实现反选逻辑

01.拓展数组功能

  1. Array.prototype.remove = function (val) {
  2. var index = this.indexOf(val);
  3. if (index > -1) {
  4. this.splice(index, 1);
  5. }
  6. };

02.选中逻辑

  1. // initEvent
  2. ShopCar.prototype.initEvent = function(){
  3. var that = this;
  4. // 绑定选中时间
  5. $(".goodsCheck").click(function(){
  6. // this点击谁就是谁
  7. if(this.checked){ // 如果选中,则添加
  8. that.check(this);
  9. }else{// 否则,删除商品
  10. that.uncheck(this);
  11. }
  12. })
  13. }

03.取消选中

  1. // 取消选中
  2. ShopCar.prototype.uncheck = function (dom) {
  3. var shopInfo = $(dom).parents('.shop-info');
  4. var id = shopInfo.data('id');
  5. for(var i=0; i<this.productArr.length; i++){
  6. if(this.productArr[i]['id'] === id){
  7. this.productArr.remove(this.productArr[i]);
  8. this.updateTotalPrice();
  9. }
  10. }
  11. }

实现添加、减少功能

01.实现添加

  1. // 添加num
  2. ShopCar.prototype.plus = function (dom) {
  3. var shopInfo = $(dom).parents('.shop-info');
  4. var id = shopInfo.data('id');
  5. var name = shopInfo.find('h4').text();
  6. var price = parseInt(shopInfo.find('.price').text());
  7. var num = +shopInfo.find('.num').text();
  8. num++;
  9. shopInfo.find('.num').text(num)
  10. // isChecked[0].checked 原生的用法
  11. // jq用法 isChecked.is(':checked')
  12. // isChecked[0] jq数组转换为原生
  13. // dom对象转换为jq, $(DOM)
  14. var checkDom = shopInfo.find('.goods-check').is(':checked');
  15. if(checkDom){// 如何选中,则更新num
  16. // 怎么改变
  17. for(var i=0; i<this.productArr.length;i++){
  18. if(this.productArr[i]['id'] === id){
  19. var obj = this.productArr[i];
  20. obj.num = num;
  21. }
  22. }
  23. this.updateTotalPrice();
  24. }else{
  25. }
  26. console.log(name,price,num ,checkDom);
  27. }

实现全选和反选

01.全选和反选

  1. // 绑定店铺全选
  2. $(".shopCheck").click(function(){
  3. var isCheck = $(this).is(':checked');
  4. // console.log(isCheck);
  5. var shopGroupItem = $(this).parents('.shop-group-item');
  6. var shopInfoArr = shopGroupItem.find('.shop-info');
  7. var newArr = [];
  8. for(var i=0; i<shopInfoArr.length; i++){
  9. var shopInfo = $(shopInfoArr[i]);
  10. var checkDom = shopInfo.find('.goodsCheck');
  11. if(isCheck){
  12. checkDom.prop('checked', true);
  13. // Product(id, name, price, num)
  14. var id = shopInfo.data('id');
  15. console.log(shopInfo.data('id'));
  16. var name = shopInfo.find('h4').text();
  17. var price = +shopInfo.find('.price').text();
  18. var num = +shopInfo.find('.num').text();
  19. var pro = new Product(id, name, price, num);
  20. newArr.push(pro);
  21. // $(":checkbox").prop("checked", true);
  22. }else{
  23. checkDom.prop('checked', false);
  24. }
  25. // checkDom.checked = true;
  26. }
  27. that.productArr = newArr; // 把数据替换原来的数据
  28. that.updateTotalPrice();
  29. })

02.是否全选,受到单个选中 影响

  1. // 判断是否已经全选
  2. ShopCar.prototype.checkAll = function(){
  3. var checkList = $(".goodsCheck");
  4. var flag = false;
  5. checkList.each(function(index,item){
  6. //console.log(this)
  7. var isCheck = $(this).is(':checked');
  8. if (!isCheck){//只要有一个未选中,则全选不选中
  9. flag = true;
  10. }
  11. })
  12. if(flag){//表示有一个未选中
  13. $(".shopCheck").prop('checked',false);
  14. }else{
  15. $(".shopCheck").prop('checked', true);
  16. }
  17. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注