欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue实现购物车功能(商品分类)

程序员文章站 2022-05-25 22:05:33
本文实例为大家分享了vue实现购物车功能的具体代码,供大家参考,具体内容如下new vue({ el: "#app", data: { cindex: 0, lists: [ { title: "...

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

new vue({
 el: "#app",
 data: {
 cindex: 0,
 lists: [
  {
  title: "推荐商品",
  goods: [
   {
   id: 0,
   img: './images/goods.png',
   name: '散称樱桃1',
   price: '10.00',
   num: 0
   },
   {
   id: 1,
   img: './images/goods.png',
   name: '散称樱桃1',
   price: '10.00',
   num: 0
   }
 
  ]
  },
  {
  title: "推荐商品",
  goods: [
   {
   id: 2,
   img: './images/goods.png',
   name: '散称樱桃2',
   price: '20.00',
   num: 0
   },
   {
   id: 3,
   img: './images/goods.png',
   name: '散称樱桃2',
   price: '20.00',
   num: 0
   },
  ]
  },
 ],
 cararr: [],
 appearcar: false,
 },
 computed:{
 alltotal:function () {
  var alltotal = 0;
  var allnum = 0;
  for(var i = 0,len = this.lists.length;i<len;i++){
  for(var j = 0,goodslen = this.lists[i].goods.length;j<goodslen;j++){
   alltotal += parseint(this.lists[i].goods[j].num)*parsefloat(this.lists[i].goods[j].price);
   allnum += parseint(this.lists[i].goods[j].num);
  }
  }
  return [alltotal,allnum]
 },
 },
 methods: {
 lefttap: function (index) {
  this.cindex = index
 },
 showcar: function () {
  if (this.cararr.length == 0) {
  this.appearcar = false
  } else {
  this.appearcar = !this.appearcar
  }
 
 },
 //商品数量加
 addnum: function (data) {
  data.num++;
  var cararr = this.cararr;
  var iscar = false;
  if (cararr == 0) {
  cararr.push(data)
  }
  for (var i = 0, len = cararr.length; i < len; i++) {
  if (data.id == cararr[i].id) {
   iscar = true
  }
  }
  if (!iscar) {
  cararr.push(data)
  }
 },
 reducenum: function (data) {
  if (data.num <= 0) return;
  var cararr = this.cararr;
  data.num--;
  var iscar = false;
  for (var i = 0, len = cararr.length; i < len; i++) {
  if (cararr[i].num <= 0) {
   cararr.splice(i, 1)
  }
  }
 },
 caraddnum: function (index) {
  this.cararr[index].num++
 },
 carreducenum: function (index) {
  this.cararr[index].num--;
  if (this.cararr[index].num <= 0) {
  this.cararr.splice(index, 1)
  }
  if (this.cararr.length == 0) {
  this.appearcar = !this.appearcar
  }
 },
 // 清空
 delcar: function () {
  confirm('确定清空购物车吗?');
  var cur = this.cindex;
  var goods = this.lists[cur].goods
  if (true) {
  this.cararr = [];
  this.appearcar = !this.appearcar;
  for(var i = 0,len = this.lists.length;i<len;i++){
   for(var j = 0,goodslen = this.lists[i].goods.length;j<goodslen;j++){
   this.lists[i].goods[j].num=0;
   }
  }
  }
 },
 settlement:function () {
  if(this.alltotal[0]<=0){
  console.log('我不动')
  }else{
  window.location.href = 'cashierpay.html'
  }
 }
 }
})

html页面

<div id="app" v-cloak>
 <div class='box'>
 <div class='boxleft'>
  <div class='boxla'>
  <img src='./images/sm.png'/>
  <span>扫码</span>
  </div>
  <ul class='boxlbox'>
  <template v-for="(item,index) in lists">
   <li @click='lefttap(index)' :class="{boxlb_cheak:cindex==index}" class='boxlb'>
   <span class='boxlb_inner' :class="{boxlb_inner_cheak:cindex==index}">{{item.title}}</span>
   </li>
  </template>
  </ul>
 </div>
 <ul class='boxright'>
  <li class='boxri' v-for="(item,index) in lists[cindex].goods">
  <img class='boxri_img' :src='item.img'/>
  <div class='boxri_text'>
   <div class='boxrit_left'>
   <p class='boxritl_name'>{{item.name}}</p>
   <p class='boxritl_price'>¥{{item.price}}</p>
   </div>
   <div class='boxrit_right'>
   <img @click='reducenum(item)' v-show="item.num>0" class='boxrit_right_img' src='./images/minus.png'/>
   <span class='boxrit_right_num' v-show="item.num>0">{{item.num}}</span>
   <img @click='addnum(item)' class='boxrit_right_img' src='./images/add.png'/>
   </div>
  </div>
  </li>
 </ul>
 </div>
 <footer class='footer'>
 <div class='cartimgbox'>
  <img class='cartimg' src='./images/car.png' @click='showcar'/>
  共{{alltotal[1]}}件商品
 </div>
 <div class='footerb'>¥{{alltotal[0]}}</div>
 <div @click='settlement' class='footerc'>结算</div>
 </footer>
 <!-- 购物车 -->
 
 <div class='shop-car-mask' v-show="appearcar">
 <div class='shop-com'>
  <div class='shop-title'>
  <span>已选商品</span>
  <span @click="delcar">清空</span>
  </div>
  <ul class="car-shoplist">
  <template v-for='(item,index) in cararr'>
   <li class='car-list'>
   <div class='car-img'>
    <img :src='item.img'/>
   </div>
   <div class='car-name'>{{item.name}}</div>
   <div class='car-num'>¥{{item.price}}</div>
   <div class='boxrit_right2'>
    <img @click='carreducenum(index)' class='boxrit_right_img2' src='./images/minus.png'/>
    <span class='boxrit_right_num2'>{{item.num}}</span>
    <img @click='caraddnum(index)' class='boxrit_right_img2' src='./images/add.png'/>
   </div>
   </li>
  </template>
  </ul>
 </div>
 </div>
</div>

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