vue实现淘宝购物车功能


Posted in Javascript onApril 20, 2020

本文实例为大家分享了vue实现淘宝购物车的具体代码,供大家参考,具体内容如下

淘宝购物车功能,效果如下图

vue实现淘宝购物车功能

非常简单的逻辑,没有做代码的封装,代码如下

<div class="list-container">
    <div class="top-ops">
        <div>
          <img src="../../../static/images/HomeRecommendShopInfoAdress@2x.png" alt="">
          <span>浙江省杭州市...</span>
        </div>
        <div class="ops">
          <span v-if="cartStatus === 'account'" @click="cartStatus = 'edit'">编辑商品</span>
          <span v-if="cartStatus === 'edit'" @click="cartStatus = 'account'">完成</span>
        </div>
    </div>
    <div class="paddingB200">
      <div class="shop-list" v-for="(item,index) in cartShops" :key="index">
        <div class="shop-name">
          <label>
            <input type="checkbox"
              name="shopRadio"
              :value="item.productShopId"
              @click="shopCheck($event,cartShops)"
            class="disN">
            <b></b>
          </label>
          <div>
            <div>
              <img src="../../../static/images/mall@2x.png" alt="">
              <span class="name">{{item.shopName}}</span>
            </div>
            <span>
              <img src="../../../static/images/jtxq@2x.png" alt="">
            </span>
          </div>
        </div>
        <div class="goods-list" v-for="(goods,goodsIndex) in item.detailLists" :key="goodsIndex">
          <label>
            <input type="checkbox"
              name="goodRadio"
              :price="goods.price"
              :num="goods.number" :dataId="item.productShopId"
              :value="goods.cartDetailId"
              @click="goodsCkeck($event,item.detailLists,cartShops,item.productShopId)"
            class="disN">
            <b></b>
          </label>
          <div class="middle">
            <img :src="goods.reportImage" alt="">
            <div>
              <p class="name">{{goods.name}}</p>
              <p class="spec">{{goods.specifications}}</p>
              <p class="tab">
                <img src="../../../static/images/lsspbq@2x.png" alt="">
              </p>
            </div>
          </div>
          <div class="right">
            <p class="price">¥{{goods.price}}</p>
            <p class="num">X{{goods.number}}</p>
            <p class="caculate">
              <span class="mark" @click="numDecrease(goods.number)"></span>
              <span class="beeforCacul">{{goods.number}}</span>
              <span class="cacul" :num="goods.cartDetailId">{{goods.number}}</span>
              <span class="mark" @click="numAdd(goods.number)"></span>
            </p>
          </div>
        </div>
      </div>
      <div class="edit" v-if="cartStatus === 'edit'">
        <label>
          <input type="checkbox" name="allRadio" class="disN" @click="allCheck($event)">
          <b></b>
          <span>全选</span>
        </label>
        <span class="delet">删除(3)</span>
      </div>
      <div class="gotopay" v-if="cartStatus === 'account'">
        <label>
          <input type="checkbox" name="allRadio" class="disN" @click="allCheck($event)">
          <b></b>
          <span class="marginR40">全选</span>
          <span>合计:</span>
          <span class="sum">¥{{sumPrice.toFixed(2)}}</span>
        </label>
        <span class="delet" @click="cauSum">去结算({{totalNumber}})</span>
      </div>
    </div>
</div>
export default {
  components: {
     
  },
  name: "life",
  data() {
    return {
      cartStatus:"account", //购物车状态,account结算,edit删除编辑状态
      cartShops: [], //店铺列表
      sumPrice:0, //合计金额
      totalNumber: 0, //总数
      shopList:[], //店铺列表
      goodsList:[], //商品列表
    };
  },
  watch: {
     
  },
  mounted() {
    this.getCartDetail();           
  },
  methods: {
    //购物车列表
    getCartDetail: function(){
      this.$http.get("api/product/v1/getCartDetail").then( res => {
        if(res.data.code === 200){
          //console.log(res.data.data)
          this.cartShops = res.data.data.cartShops;
        }else{
          Toast(res.data.msg);
        }
      }).catch( error => {
        console.log(error)
      })
    },
    //商品选择
    goodsCkeck: function(event,goodsList,shopList,shopId){                 
      //商品列表+-,店铺是否checked(店铺列表+-),全选是否checked
      var input = document.getElementsByTagName('input')
      if(event.currentTarget.checked){
        this.goodsList.push(String(event.currentTarget.value));
        //如果店铺内所有商品全选,店铺选中
        var newArr = this.goodsList;
        var tt = goodsList.every(function(itemValue){
          return (newArr.indexOf(String(itemValue.cartDetailId)) != -1)
        })
        if(tt){
          //店铺内全选,店铺checked,店铺列表+
          for(var i = 0;i<input.length;i++){
            if(input[i].value == shopId){
              input[i].checked = true;
            }
          }
          this.shopList.push(String(shopId)); //防止shopid是number类型造成麻烦
          //如果所有店铺都全选,则全选按钮checked
          if(this.shopList.length === shopList.length){
            //所有店铺全选
            for(var i = 0;i<input.length;i++){
              if(input[i].name == 'allRadio'){
                input[i].checked = true;
              }
            }
          }
        }
      }else{
        //商品列表--
        this.goodsList.splice(this.goodsList.indexOf(event.currentTarget.value),1)
        //如果店铺checked,则取消,店铺列表--
        for(var i = 0;i<input.length;i++){
          if(input[i].value == shopId){
            if(input[i].checked){
              input[i].checked = false;
              this.shopList.splice(this.shopList.indexOf(String(shopId)),1); //防止shopid是number类型造成麻烦
            }
          }
          //任意一个不选,全选取消
          if(input[i].name == 'allRadio'){
            input[i].checked = false;
          }
        }
      }
      //计算总价和数量
      this.caculate();
    },
    //店铺选择
    shopCheck: function(event,shopList){
      //店铺选中则对应商品全选,否则全不选
      //console.log(event.currentTarget)
      var input = document.getElementsByTagName('input')
      if(event.currentTarget.checked){
        //店铺列表+,店铺checked,店铺内商品全checked,商品列表++
        //console.log(this.shopList)
        this.shopList.push(String(event.currentTarget.value));
        //店铺内商品全checked
        for(var i = 0;i<input.length;i++){
          if(input[i].getAttribute('dataId') == event.currentTarget.value){
            //将没有选中的checked,并加入列表,去重
            if(!input[i].checked){
              input[i].checked = true;
              //商品列表++
              this.goodsList.push(String(input[i].value))
            }
          }
        }
        //所有店铺全选
        if(this.shopList.length === shopList.length){
          for(var i = 0;i<input.length;i++){
            if(input[i].name == 'allRadio'){
              input[i].checked = true;
            }
          }
        }
      }else{
        //店铺取消checked,店铺列表--,店铺内所有商品取消checked,商品列表--
        this.shopList.splice(this.shopList.indexOf(String(event.currentTarget.value)),1);
        //店铺内所有商品取消checked
        for(var i = 0;i<input.length;i++){
          if(input[i].getAttribute('dataId') == event.currentTarget.value){
            input[i].checked = false;
            //商品列表--
            this.goodsList.splice(this.goodsList.indexOf(input[i].value),1);
          }
          //任意一个不选,全选取消
          if(input[i].name == 'allRadio'){
            input[i].checked = false;
          }
        }
      }
      //计算总价和数量
      this.caculate();
    },
    //所有全选
    allCheck: function(event){
      var input = document.getElementsByTagName('input')
      if(event.currentTarget.checked){
        //全选checked,所有店铺checked,店铺列表++,所有商品checked,商品列表++
        for(var i = 0;i<input.length;i++){
          //去重
          if(!input[i].checked){
            input[i].checked = true;
            if(input[i].name == 'shopRadio'){
              this.shopList.push(String(input[i].value))
            }
            if(input[i].name == 'goodRadio'){
              this.goodsList.push(String(input[i].value))
            }
          }
        }
      }else{
        //全不选取消checked,店铺全部取消checked,店铺列表清空,所有商品取消checked,商品列表清空
        for(var i = 0;i<input.length;i++){
          input[i].checked = false;
          this.shopList = [];
          this.goodsList = [];
        }
      }
      //计算总价和数量
      this.caculate();
    },
    //计算总金额总数量
    caculate: function(){
      var input = document.getElementsByTagName('input');
      var newArr = [];
      for(var i = 0;i<input.length;i++){
        if(input[i].name == 'goodRadio' && input[i].checked){
          var num = input[i].parentNode.parentNode.children[2].children[2].children[2].innerHTML;
          newArr.push(
            {
              'price': input[i].getAttribute('price'),
              'num': num
            }
          )
        }
      }
      this.totalNumber = newArr.length;
      //归零
      this.sumPrice = 0;
      for(var j = 0,len = newArr.length;j<len;j++){
        this.sumPrice += newArr[j].price * newArr[j].num;
      }
    },
    //数量减小
    numDecrease: function(num){
      //如果当前input选中,则修改数量计算价格,如果当前input没有选中,则修改数量不计算价格
      var spanList = event.currentTarget.parentNode.children;
      for(var i = 0,len = spanList.length;i<len;i++){
        if(spanList[i].getAttribute("class") == 'beeforCacul'){
          spanList[i].style.display = 'none';
        }
        if(spanList[i].getAttribute("class") == 'cacul'){
          spanList[i].style.display = 'block';
          var caculNum = spanList[i].innerHTML;
          if(caculNum < 2){
            Toast('宝贝不能再少了哦');
          }else{
            caculNum --
            spanList[i].innerHTML = caculNum;
          }
        }
      }
      if(event.currentTarget.parentNode.parentNode.parentNode.children[0].children[0].checked){
        this.caculate();
      }
    },
    //数量增加
    numAdd: function(num){
      var spanList = event.currentTarget.parentNode.children;
      //console.log(event.currentTarget.parentNode.children)
      for(var i = 0,len = spanList.length;i<len;i++){
        if(spanList[i].getAttribute("class") == 'beeforCacul'){
          spanList[i].style.display = 'none';
        }
        if(spanList[i].getAttribute("class") == 'cacul'){
          spanList[i].style.display = 'block';
          var caculNum = spanList[i].innerHTML;
          caculNum ++;
          spanList[i].innerHTML = caculNum;
        }
      }
      if(event.currentTarget.parentNode.parentNode.parentNode.children[0].children[0].checked){
        this.caculate();
      }
    },
    //去结算
    cauSum:function(){
      if(this.sumPrice === 0){
        Toast('您还没有选择宝贝哦');
      }else{
        this.$router.push('/cart/order')
      }
    },
  }
};

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
你的编程语言可以这样做吗?
Sep 07 Javascript
asp 的 分词实现代码
May 24 Javascript
javascript的几种写法总结
Sep 30 Javascript
js图片延迟加载(Lazyload)三种实现方式
Mar 01 Javascript
如何编写jquery插件
Mar 29 jQuery
vue-cli+webpack在生成的项目中使用bootstrap实例代码
May 26 Javascript
EL表达式截取字符串的函数说明
Sep 22 Javascript
JS实现点击循环切换显示内容的方法
Oct 19 Javascript
vue中的event bus非父子组件通信解析
Oct 27 Javascript
深入浅析ng-bootstrap 组件集中 tabset 组件的实现分析
Jul 19 Javascript
微信小程序tabBar 返回tabBar不刷新页面
Jul 25 Javascript
p5.js实现故宫橘猫赏秋图动画
Oct 23 Javascript
javascript利用键盘控制小方块的移动
Apr 20 #Javascript
vue实现购物车的监听
Apr 20 #Javascript
详解微信小程序工程化探索之webpack实战
Apr 20 #Javascript
Vue中el-form标签中的自定义el-select下拉框标签功能
Apr 20 #Javascript
javascript设计模式 ? 中介者模式原理与用法实例分析
Apr 20 #Javascript
javascript设计模式 ? 命令模式原理与用法实例分析
Apr 20 #Javascript
JS实现横向跑马灯效果代码
Apr 20 #Javascript
You might like
php中的三元运算符使用说明
2011/07/03 PHP
说说PHP的autoLoad自动加载机制
2012/09/27 PHP
linux系统下php安装mbstring扩展的二种方法
2014/01/20 PHP
php+js实现异步图片上传实例分享
2014/06/02 PHP
php生成图片验证码的方法
2016/04/15 PHP
PHP使用OB缓存实现静态化功能示例
2019/03/23 PHP
写了10年的Javascript也未必全了解的连续赋值运算
2011/03/25 Javascript
JS父页面与子页面相互传值方法
2014/03/05 Javascript
JS中的构造函数详细解析
2014/03/10 Javascript
jQuery中closest()函数用法实例
2015/01/07 Javascript
JavaScript判断FileUpload控件上传文件类型
2015/09/28 Javascript
分享自己用JS做的扫雷小游戏
2016/02/17 Javascript
深入浅析Extjs中store分组功能的使用方法
2016/04/20 Javascript
JQuery validate插件Remote用法大全
2016/05/15 Javascript
JS实现pasteHTML兼容ie,firefox,chrome的方法
2016/06/22 Javascript
jQuery 插件封装的方法
2016/11/16 Javascript
走进javascript——不起眼的基础,值和分号
2017/02/24 Javascript
vue.js 初体验之Chrome 插件开发实录
2017/05/13 Javascript
VueJs 搭建Axios接口请求工具
2017/11/20 Javascript
vue如何判断dom的class
2018/04/26 Javascript
vue新vue-cli3环境配置和模拟json数据的实例
2018/09/19 Javascript
layui table设置某一行的字体颜色方法
2019/09/05 Javascript
es6函数name属性功能与用法实例分析
2020/04/18 Javascript
解决Vue-cli无法编译es6的问题
2020/10/30 Javascript
[01:39]2014DOTA2国际邀请赛 Newbee经理CU专访队伍火力全开
2014/07/15 DOTA
在Python中利用Pandas库处理大数据的简单介绍
2015/04/07 Python
python学习教程之Numpy和Pandas的使用
2017/09/11 Python
读取json格式为DataFrame(可转为.csv)的实例讲解
2018/06/05 Python
解决PyCharm的Python.exe已经停止工作的问题
2018/11/29 Python
python  ceiling divide 除法向上取整(或小数向上取整)的实例
2019/12/27 Python
浅谈ROC曲线的最佳阈值如何选取
2020/02/28 Python
Python Django view 两种return的实现方式
2020/03/16 Python
python解释器安装教程的方法步骤
2020/07/02 Python
python开发入门——set的使用
2020/09/03 Python
巧用HTML5给按钮背景设计不同的动画简单实例
2016/08/09 HTML / CSS
CSS3 天气图标动画效果
2021/04/06 HTML / CSS