vue实现购物车结算功能

用vue做的购物车结算的功能,供大家参考,具体内容如下

代码:

<!-- 占位 -->
<template>
 <div>
  <div class="product_table">
   <div class="product_info">商品信息</div>
   <div class="product_info">商品金额</div>
   <div class="product_info">商品数量</div>
   <div class="product_info">总金额</div>
   <div class="product_info">编辑</div>
  </div>
  <div class="product_table" v-for="(item,index) in getProductList" :key="index">
   <div style="width:20px;height:20px;border-radius:10px;border:1px solid black;" @click="checkSingle(item)" :class="{checked:item.makeChoose}"></div>
   <div class="product_info">{{item.productName}}</div>
   <div class="product_info">{{item.productPrice}}</div>
   <span @click="changeNumber(item,1)">+</span>
   <input type="text" v-model="item.prductQty" style="width: 30px;">
   <span @click="changeNumber(item,-1)">-</span>
   <div class="product_info">{{item.productPrice*item.prductQty}}</div>
   <div class="product_info" @click="deleteProduct(index)">删除</div>
  </div>
  <div style="width:20px;height:20px;border-radius:10px;border:1px solid black;margin-top:10px" @click="checkAll()" :class="{checked:checkAllItem}"></div>
  <div>总价格:{{totalPrice}}</div>
 </div>
</template>
<script>
 import Vue from 'vue'
 export default {
 name: 'side-bar-placeholder',
 data () {
  return {
  getProductList:[
   {
    productName:'西瓜',
    productPrice:100,
    prductQty:3
   },
   {
    productName:'南瓜',
    productPrice:50,
    prductQty:2
   },
   {
    productName:'苹果',
    productPrice:300,
    prductQty:3
   },
  ],
  totalPrice:0, //总金额
  checkAllItem:false, //全部选中
  checkedList:[] //选中的数
  }
 },
 methods:{
  //删除某一项
  deleteProduct:function (index) {
  this.getProductList.splice(index,1)
  this.calcTotalPrice() //这里要注意,当某一项删除时,如果你选中了,这里也是要做计算总价格的
  },
  //修改数量
  changeNumber:function (number,add) {
   if(add<0){
   number.prductQty--;
   if(number.prductQty<'1'){ //因为数量最低是1
    number.prductQty=1
   }
   }else{
    number.prductQty++;
   }
   this.calcTotalPrice()
  },
  //选中单个的
  checkSingle:function (item){
   if(typeof item.makeChoose=='undefined'){ //这里要注意,因为checked字段根本不在this.getProductList里面,所以你要自己赋值进去
    Vue.set(item, 'makeChoose',true) //这里应该设为true
   }else{
    item.makeChoose=!item.makeChoose
   }
   this.calcTotalPrice()
  },
  //选中所有的
  checkAll:function (){
   this.checkAllItem=!this.checkAllItem
   var _this=this
   if(this.checkAllItem){
   this.getProductList.forEach(element => {
    if(typeof element.makeChoose=='undefined'){
     Vue.set(element, 'makeChoose',_this.checkAllItem) //让每一小项跟随checkall来变化
    }else{
    element.makeChoose=_this.checkAllItem
    }
   });
   }else{
   this.getProductList.forEach(element => {
    if(typeof element.makeChoose=='undefined'){
     Vue.set(element, 'makeChoose',_this.checkAllItem)
    }else{
    element.makeChoose=_this.checkAllItem
    }
   });
   }
   this.calcTotalPrice()
  },
  //计算总金额
  calcTotalPrice:function () {
   var _this=this
   this.totalPrice=0
   this.getProductList.forEach((element,index) => {
   if(element.makeChoose){
    _this.totalPrice+=element.productPrice*element.prductQty //这里是一个累加的过程,所以要用+=
   }
   });
  },
  //让页面一进来就处于选中的状态
  makeAllChecked:function () {
  this.getProductList.forEach((item)=>{
   if(typeof item.makeChoose=='undefined'){
    Vue.set(item, 'makeChoose',true)
   }
  })
  }
 } ,
 watch:{
  //如果全部选中,那么全部选中的按钮应该变绿,如果一项不是,应该变空
  getProductList:{
   handler:function (item) {
   this.checkedList=this.getProductList.filter((element)=>{
    return element.makeChoose==true;
   })
   //选中数<总数据
   if(this.checkedList.length<this.getProductList.length){
    this.checkAllItem=false
   }else{
    this.checkAllItem=true
   }
   },
   deep:true //这个deep:true一定要写,不然肯定不会时时变化的
  }
 } ,
 created:function (){
  this.makeAllChecked()
 }
 }
</script>
<style lang="less" scoped>
.product_table{
 display: flex;
 width: 100%;
}
.product_info{
 flex:1;
}
.checked{
 background-color:green;
}
</style>

这个代码实现了什么?
1.在点击加减时每个产品的总价变化,所有产品的总价变化
2.选中时才会结算
3.如果全部选中了每个子项,全部选中按钮会变绿,如果有一项不选中,那么会变白
4.一般的购物车,我希望他一进来就是checked的状态,提高购买性
5.当我删除某一项时,如果这一项是已经checked了的,也要让他在计算总价时重新计算.

ps:最后一行的按钮是全部选中哦,或者是全部取消,忘记写了

(0)

相关推荐

  • vuejs手把手教你写一个完整的购物车实例代码

    由于我们公司是主营业务是海淘,所以每个项目都是类似淘宝天猫之类的商城,那么购物车就是一个重点开发功能模块.介于之前我都是用jq来写购物车的,这次就用vuejs来写一个购物车.下面我就从全选,数量控制器,运费,商品金额计算等方法来演示一下一个能用在实际场景的购物车是怎么做出来的以及记录一下这次用vuejs踩过的坑. 1.一层数据结构-全选 下面这段代码和vuejs官网里面checkbox绑定很像.不明白的可以直接上vuejs官网看看. <!DOCTYPE html> <html lang=

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

    本文实例为大家分享了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: '

  • 基于Vuejs实现购物车功能

    本文实例为大家分享了Vuejs购物车实现代码,供大家参考,具体内容如下 html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>我的vue购物车</title> <link rel="stylesheet" href="css/bootstrap.min.css&q

  • vue实现购物车案例

    本文实例为大家分享了vue实现购物车的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-

  • vue实现购物车加减

    本文实例为大家分享了vue实现购物车加减的具体代码,供大家参考,具体内容如下 通常我们会在模板中绑定表达式,模板是用来描述视图结构的.如果模板中的表达式存在过多的逻辑,模板会变得臃肿不堪,维护变得非常困难.因此,为了简化逻辑,当某个属性的值依赖于其他属性的值时,我们可以使用计算属性. 那么什么是计算属性呢? 计算属性就是当其依赖属性的值发生变化是,这个属性的值会自动更新,与之相关的DOM部份也会同步自动更新. 实现的效果图如下: 我是使用了bootstrap跟Vue去完成这个效果的. 首先导入包

  • vue购物车插件编写代码

    本文实例为大家分享了vue购物车插件的具体代码,供大家参考,具体内容如下 先上效果图 下面相关代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello MUI</title> <meta name="viewport" content="width=device-width, initial-sca

  • Vue.js搭建移动端购物车界面

    本文介绍了如何使用Vue搭建一个移动端购物车界面,最终实现的功能包括: 1. 选择要最终购买的物品 2. 选择每件物品购买的数量 3.  实时更新所选择物品的总价格 HTML部分 首先给出HTML部分代码和注释,显示了整个界面的结构. <body> <div class="checkout"> <div id="app"> <div class="container"> <div class=

  • vue实现商城购物车功能

    本文实例为大家分享了vue实现商城购物车功能的具体代码,供大家参考,具体内容如下 首先,先上最终的效果图 效果并不是很好看,但是这不是重点. 首先,我们先看下布局: <template> <div class="shopcar" id="demo04"> <div class="header-title"> <h3>购物车</h3> </div> <div class=

  • Vue实现购物车场景下的应用

    本文实例为大家分享了Vue在购物车场景下的应用,供大家参考,具体内容如下 购物车场景需求: 1. 商品.店铺.购物车的选择 2. 商品删除 关键代码 测试数据 var _list = [{ checked: false, goods: [{ name: "商品1", price: 23, checked: false }] }, { checked: false, goods: [{ name: "商品2", price: 20, checked: false },

  • vue.js实现简单购物车功能

    本文实例为大家分享了vue.js实现简单购物车的具体代码,供大家参考,具体内容如下 这次我将给大家带来一个vue.js实现购物车的小项目,如有不足请严厉指出. 购物车功能有:全选和选择部分商品,选中商品总价计算,商品移除,以及调整购买数量等功能. js主要有以下方法 加函数,减函数,手动修改数量判断库存函数,总价格计算函数,单选事件,全选事件,一共分为6个事件 具体效果如下图 代码在这里 main.js 'use strict' var app = new Vue({ el: '#app', d

随机推荐