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 相关文章推荐
关于 byval 与 byref 的区别分析总结
Oct 08 Javascript
解决Extjs上传图片无法预览的解决方法
Mar 22 Javascript
基于jquery的时间段实现代码
Aug 02 Javascript
JavaScript字符串常用类使用方法汇总
Apr 14 Javascript
JQuery包裹DOM节点的方法
Jun 11 Javascript
jquery实现手机号码选号的方法
Jul 31 Javascript
JQuery异步加载PartialView的方法
Jun 07 Javascript
第一次动手实现bootstrap table分页效果
Sep 22 Javascript
信息滚动效果的实例讲解
Sep 18 Javascript
Vue模板语法中数据绑定的实例代码
May 17 Javascript
原生JS封装vue Tab切换效果
Apr 28 Vue.js
js不常见操作运算符总结
Nov 20 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和ACCESS写聊天室(七)
2006/10/09 PHP
php 伪静态之IIS篇
2014/06/02 PHP
VPS中使用LNMP安装WordPress教程
2014/12/28 PHP
php ajax实现文件上传进度条
2016/03/29 PHP
PHP实现QQ快速登录的方法
2016/09/28 PHP
PHP简单实现正则匹配省市区的方法
2018/04/13 PHP
JS 页面自动加载函数(兼容多浏览器)
2009/05/18 Javascript
js 获取服务器控件值的代码
2010/03/05 Javascript
自写的一个jQuery圆角插件
2010/10/26 Javascript
artDialog+plupload实现多文件上传
2016/07/19 Javascript
Vue2.0 UI框架ElementUI使用方法详解
2017/04/14 Javascript
浅谈layer弹出层按钮颜色修改方法
2019/09/11 Javascript
jquery实现有过渡效果的tab切换
2020/07/17 jQuery
python插入排序算法的实现代码
2013/11/21 Python
python函数返回多个值的示例方法
2013/12/04 Python
Python学习笔记之os模块使用总结
2014/11/03 Python
python中enumerate函数用法实例分析
2015/05/20 Python
使用pandas读取文件的实现
2019/07/31 Python
python装饰器练习题及答案
2019/11/01 Python
Python接口测试环境搭建过程详解
2020/06/29 Python
python--shutil移动文件到另一个路径的操作
2020/07/13 Python
pandas统计重复值次数的方法实现
2021/02/20 Python
CSS3实现曲线阴影和翘边阴影
2016/05/03 HTML / CSS
教育学专业毕业生的自我评价
2013/11/21 职场文书
迟到检讨书大全
2014/01/25 职场文书
技校个人求职信范文
2014/01/25 职场文书
庆祝教师节活动方案
2014/01/31 职场文书
校园元旦活动总结
2014/07/09 职场文书
计算机科学与技术专业求职信
2014/09/03 职场文书
营业用房租赁协议书
2014/11/26 职场文书
机动车交通事故协议书
2015/01/29 职场文书
道歉信范文
2015/05/12 职场文书
python中的None与NULL用法说明
2021/05/25 Python
python 中的@运算符使用
2021/05/26 Python
详解非极大值抑制算法之Python实现
2021/06/28 Python
MongoDB安装使用并实现Python操作数据库
2021/06/28 MongoDB