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中的107个基础知识收集整理 推荐
Mar 29 Javascript
JS实现QQ图片一闪一闪的效果小例子
Jul 31 Javascript
jquery设置按钮停顿3秒不可用
Mar 07 Javascript
jQuery中slideUp()方法用法分析
Dec 24 Javascript
JS实现点击上移下移LI行数据的方法
Aug 05 Javascript
阻止表单提交按钮多次提交的完美解决方法
May 16 Javascript
WebSocket的通信过程与实现方法详解
Apr 29 Javascript
深入理解JavaScript 中的匿名函数((function() {})();)与变量的作用域
Aug 28 Javascript
详解使用uni-app开发微信小程序之登录模块
May 09 Javascript
webpack HappyPack实战详解
Oct 08 Javascript
微信小程序聊天功能的示例代码
Jan 13 Javascript
vue中 v-for循环的用法详解
Feb 19 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
15个小时----从修改程序到自己些程序
2006/10/09 PHP
分享一个PHP数据流应用的简单例子
2012/06/01 PHP
php+mysqli实现将数据库中一张表信息打印到表格里的方法
2015/01/28 PHP
php获取网站根目录物理路径的几种方法(推荐)
2017/03/04 PHP
Js之软键盘实现(js源码)
2007/01/30 Javascript
理解Javascript_10_对象模型
2010/10/16 Javascript
7款吸引人眼球的jQuery/CSS3特效实例分享
2013/04/25 Javascript
javascript if条件判断方法小结
2014/05/17 Javascript
js匿名函数的调用示例(形式多种多样)
2014/08/20 Javascript
JS+CSS简单树形菜单实现方法
2015/09/12 Javascript
JS实现把鼠标放到链接上出现滚动文字的方法
2016/04/06 Javascript
React Native实现简单的登录功能(推荐)
2016/09/19 Javascript
Jquery Easyui进度条组件Progress使用详解(8)
2020/03/26 Javascript
微信小程序 开发之快递查询功能的实现
2017/01/09 Javascript
JavaScript实现简单生成随机颜色的方法
2017/09/21 Javascript
详解Chai.js断言库API中文文档
2018/01/31 Javascript
Vue2.0子同级组件之间数据交互方法
2018/02/28 Javascript
iview在vue-cli3如何按需加载的方法
2018/10/31 Javascript
ES6入门教程之变量的解构赋值详解
2019/04/13 Javascript
layui实现数据分页功能
2019/07/27 Javascript
解决vue项目刷新后,导航菜单高亮显示的位置不对问题
2019/11/01 Javascript
基于Django的乐观锁与悲观锁解决订单并发问题详解
2019/07/31 Python
python实现BP神经网络回归预测模型
2019/08/09 Python
python 叠加等边三角形的绘制的实现
2019/08/14 Python
wxpython绘制音频效果
2019/11/18 Python
Jupyter加载文件的实现方法
2020/04/14 Python
cookies应对python反爬虫知识点详解
2020/11/25 Python
详解Pycharm第三方库的安装及使用方法
2020/12/29 Python
Manduka官网:瑜伽垫、瑜伽毛巾和服装
2018/07/02 全球购物
世界上第一个创建了罩杯系统的美国内衣品牌:Maidenform
2019/03/23 全球购物
说出数据连接池的工作机制是什么?
2013/04/19 面试题
说一下Linux下有关用户和组管理的命令
2016/01/04 面试题
优秀信贷员先进事迹
2014/01/31 职场文书
《我要的是葫芦》教学反思
2014/02/23 职场文书
《秋天的怀念》教学反思
2016/02/17 职场文书
德生2P3收音机开箱评测
2022/04/30 无线电