Vue实现购物车功能


Posted in Javascript onApril 27, 2017

效果图:

Vue实现购物车功能

代码如下:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.0.2/css/bootstrap.min.css" rel="external nofollow" />
</head>
<body>

  <div id="app" class="container">
    <table class="table">
      <thead>
        <tr>
          <th>产品编号</th>
          <th>产品名字</th>
          <th>购买数量</th>
          <th>产品单价</th>
          <th>产品总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item , index) in message">
          <td @click="jia(index)">{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>
            <button type="button" class="btn tn-primary" @click="subtract(index)">-</button>
            <input type="text" v-model="item.quantity">
            <button type="button" class="btn tn-primary" @click="add(index)">+</button>
          </td>
          <td>{{item.price | filtermoney}}</td>
          <!--<td>{{arr[index].one}}</td>-->
          <td>{{item.price*item.quantity | filtermoney}}</td>
          <td>
            <button type="button" class="btn btn-danger" @click="remove(index)">移除</button>
          </td>
        </tr>
        <tr>
          <td>总购买价  
          </td>
          <td>
            {{animatenum | filtermoney}}
          </td>
          <td>总购买数量
          </td>
          <td>

          </td>
          <td colspan="2">
            <button type="button" class="btn btn-danger" @click="empty()">清空购物车</button>
          </td>
        </tr>
      </tbody>
    </table>

    <p v-if="message.length===0">您的购物车为空</p>
  </div>
  <script src="https://unpkg.com/tween.js@16.3.4"></script>
  <script src="https://cdn.bootcss.com/vue/2.2.3/vue.min.js"></script>
  <script>
   var vm=new Vue({
   el:"#app",
   data:{
    totalPrice:0,
    animatenum:0,
    message:[
    {
        id: 007,
        name: 'iphone5s',
        quantity: 3,
        price: 4000
     },{
        id: 1340,
        name: 'iphone5',
        quantity: 9,
        price: 3000
     },{
        id: 7758,
        name: 'imac',
        quantity: 4,
        price: 7000
     },{
        id: 2017,
        name: 'ipad',
        quantity: 5,
        price: 6000
      }
    ]
   },
   watch:{
    toComput2:function(newValue,oldValue){
    this.tween(newValue,oldValue);
    }
   },
   computed:{
    //计算总金额
    toComput2:function(){
    var vm=this;
    //每次进来要重置总金额
    vm.totalPrice=0;
    this.message.forEach(function(mess){
     vm.totalPrice+=parseInt(mess.price*mess.quantity);
    })
    return this.totalPrice;
    }
   },
   filters:{
    filtermoney:function(value){
    return '¥'+value ;
    }
   },
   mounted:function(){ 
    this.tween('97000','0');
   },
   methods:{
    //计算总数的方法为什么写在methods里面就不行?
    toComput:function(){
    var vm=this;
    vm.message.forEach(function(mess){
     vm.totalPrice+=parseInt(mess.price*mess.quantity);
    })
   return vm.totalPrice;
    },
    add:function(index){
    var vm=this;
    vm.message[index].quantity++;
    },
    subtract:function(index){
    var vm=this;
    vm.message[index].quantity--;
    if(vm.message[index].quantity<=0){
     if (confirm("你确定移除该商品?")) { 
        vm.message.splice(index,1)
      } 
    }

    },
    remove:function(index){
    var vm=this;
    if (confirm("你确定移除该商品?")) { 
        vm.message.splice(index,1)
      } 
    },
    empty:function(){
    var vm=this;
    vm.message.splice(0,vm.message.length);
    },
    jia:function(index){
    var vm=this;
    vm.arr[index].one++;
    },
    tween:function(newValue,oldValue){
   var vm=this;
   var twen=new TWEEN.Tween({animatenum:oldValue});
   function animate() {
     requestAnimationFrame(animate);  
     TWEEN.update(); 
   };
   twen.to({animatenum:newValue},750);
   twen.onUpdate(function(){
   //toFixed();保留几位小数
   vm.animatenum = this.animatenum.toFixed();
   })
   twen.start();
   animate();
  }
   }
   });

  </script> 
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
javascript 获取select下拉列表值的代码
Sep 07 Javascript
浅析jquery ajax异步调用方法中不能给全局变量赋值的原因及解决方法
Jan 10 Javascript
js实现延迟加载的方法
Jun 24 Javascript
jQuery中事件与动画的总结分享
May 24 Javascript
JavaScript的new date等日期函数在safari中遇到的坑
Oct 24 Javascript
JS字符串false转boolean的方法(推荐)
Mar 08 Javascript
AngularJS实现页面定时刷新
Mar 14 Javascript
JavaScript生成图形验证码
Aug 24 Javascript
jQuery中内容过滤器简单用法示例
Mar 31 jQuery
Vue 监听列表item渲染事件方法
Sep 06 Javascript
详解JavaScript原生封装ajax请求和Jquery中的ajax请求
Feb 14 jQuery
Vue+element 解决浏览器自动填充记住的账号密码问题
Jun 11 Javascript
js轮播图透明度切换(带上下页和底部圆点切换)
Apr 27 #Javascript
Angular.js中定时器循环的3种方法总结
Apr 27 #Javascript
浅谈js使用in和hasOwnProperty获取对象属性的区别
Apr 27 #Javascript
微信小程序 wx:for的使用实例详解
Apr 27 #Javascript
微信小程序 动态传参实例详解
Apr 27 #Javascript
微信小程序 本地数据读取实例
Apr 27 #Javascript
js模仿微信朋友圈计算时间显示几天/几小时/几分钟/几秒之前
Apr 27 #Javascript
You might like
php如何调用webservice应用介绍
2012/11/24 PHP
php ImageMagick windows下安装教程
2015/01/26 PHP
深入剖析PHP中printf()函数格式化使用
2016/05/23 PHP
jsTree 基于JQuery的排序节点 Bug
2011/07/26 Javascript
javascript中关于执行环境的杂谈
2011/08/14 Javascript
jQuery+CSS 实现随滚动条增减的汽水瓶中的液体效果
2011/09/26 Javascript
仿谷歌主页js动画效果实现代码
2013/07/14 Javascript
jquery animate实现鼠标放上去显示离开隐藏效果
2013/07/21 Javascript
判断ie的两种简单方法
2013/08/12 Javascript
jquery解析XML字符串和XML文件的方法说明
2014/02/21 Javascript
javascript实现的一个带下拉框功能的文本框
2014/05/08 Javascript
基于jquery实现的自动补全功能
2015/03/12 Javascript
初步使用bootstrap快速创建页面
2016/03/03 Javascript
改变vue请求过来的数据中的某一项值的方法(详解)
2018/03/08 Javascript
详解node登录接口之密码错误限制次数(含代码)
2019/10/25 Javascript
小程序如何定位所在城市及发起周边搜索
2020/02/11 Javascript
解决vue bus.$emit触发第一次$on监听不到问题
2020/07/28 Javascript
jquery实现异步文件上传ajaxfileupload.js
2020/10/23 jQuery
在Python中封装GObject模块进行图形化程序编程的教程
2015/04/14 Python
Python2.x版本中基本的中文编码问题解决
2015/10/12 Python
利用Python实现Windows定时关机功能
2017/03/21 Python
Python同步遍历多个列表的示例
2019/02/19 Python
python3 自动识别usb连接状态,即对usb重连的判断方法
2019/07/03 Python
pycharm通过ssh连接远程服务器教程
2020/02/12 Python
django有哪些好处和优点
2020/09/01 Python
猫途鹰:全球领先的旅游点评社区
2017/04/07 全球购物
Waterford美国官网:爱尔兰水晶制品品牌
2017/04/26 全球购物
印尼网上商店:Alfacart.com
2019/03/11 全球购物
英国在线发型和美容产品商店:Beauty Cutie
2019/04/27 全球购物
维多利亚的秘密阿联酋官网:Victoria’s Secret阿联酋
2019/12/07 全球购物
验房委托书
2014/08/30 职场文书
违反交通法规检讨书
2014/09/10 职场文书
企业法人代表证明书
2015/06/18 职场文书
学生早退检讨书(范文)
2019/08/19 职场文书
如何利用pygame实现打飞机小游戏
2021/05/30 Python
在CSS中使用when/else的方法
2022/01/18 HTML / CSS