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中利用数组实现的循环队列代码
Jan 24 Javascript
javascript基础知识大全 便于大家学习,也便于我自己查看
Aug 17 Javascript
移动设备web开发首选框架:zeptojs介绍
Jan 29 Javascript
使用jQuery实现Web页面换肤功能的要点解析
May 12 Javascript
Javascript实现图片懒加载插件的方法
Oct 20 Javascript
薪资那么高的Web前端必看书单
Oct 13 Javascript
JS实现按钮颜色切换效果
Sep 05 Javascript
php 解压zip压缩包内容到指定目录的实例
Jan 23 Javascript
详解Js里的for…in和for…of的用法
Mar 28 Javascript
微信小程序 checkbox使用实例解析
Sep 09 Javascript
js实现图片跟随鼠标移动效果
Oct 16 Javascript
javascript canvas实现简易时钟例子
Sep 05 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数组实现无限分类,不使用数据库,不使用递归.
2006/12/09 PHP
php中如何防止表单的重复提交
2013/08/02 PHP
php session劫持和防范的方法
2013/11/12 PHP
php实现zip文件解压操作
2015/11/03 PHP
php提交过来的数据生成为txt文件
2016/04/28 PHP
php数据库操作model类(使用__call方法)
2016/11/16 PHP
thinkphp框架表单数组实现图片批量上传功能示例
2020/04/04 PHP
JavaScript开发规范要求(规范化代码)
2010/08/16 Javascript
js 关键词高亮(根据ID/tag高亮关键字)案例介绍
2013/01/21 Javascript
js的隐含参数(arguments,callee,caller)使用方法
2014/01/28 Javascript
jQuery中offsetParent()方法用法实例
2015/01/19 Javascript
chrome调试javascript详解
2015/10/21 Javascript
完全深入学习Bootstrap表单
2016/11/28 Javascript
javascript常用经典算法详解
2017/01/11 Javascript
浅析jsopn跨域请求原理及cors(跨域资源共享)的完美解决方法
2017/02/06 Javascript
用node和express连接mysql实现登录注册的实现代码
2017/07/05 Javascript
javascript实现图片轮播代码
2019/07/09 Javascript
使用flow来规范javascript的变量类型
2019/09/12 Javascript
vue把输入框的内容添加到页面的实例讲解
2019/11/11 Javascript
jquery+ajax实现异步上传文件显示进度条
2020/08/17 jQuery
python中的sort方法使用详解
2014/07/25 Python
python开发之基于thread线程搜索本地文件的方法
2015/11/11 Python
Python解析、提取url关键字的实例详解
2018/12/17 Python
简单了解Python读取大文件代码实例
2019/12/18 Python
python3使用Pillow、tesseract-ocr与pytesseract模块的图片识别的方法
2020/02/26 Python
Python TestSuite生成测试报告过程解析
2020/07/23 Python
PyCharm设置注释字体颜色以及是否倾斜的操作
2020/09/16 Python
Python爬虫之Selenium实现关闭浏览器
2020/12/04 Python
Fanatics英国官网:美国体育电商
2018/11/06 全球购物
工厂总经理岗位职责
2014/02/07 职场文书
采购类个人求职的自我评价
2014/02/18 职场文书
2014年学习雷锋活动总结
2014/03/01 职场文书
班长竞选演讲稿
2014/04/24 职场文书
光荣之路观后感
2015/06/12 职场文书
公司致全体员工的感谢信
2019/06/24 职场文书
MySQL三种方式实现递归查询
2022/04/18 MySQL