Vue实现购物车功能
作者:1178291141 发布时间:2024-04-30 10:26:21
标签: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>
来源:http://www.qdfuns.com/notes/31876/829e0144532dbd08107aaa5dd48ae336.html


猜你喜欢
- 一、使用 Microsoft OLE DB Provider For ODBC 链接MySQL安装MySQL的ODBC驱动MyODBC1、为
- 对于一个给定的字符串,逆序输出,这个任务对于python来说是一种很简单的操作,毕竟强大的列表和字符串处理的一些列函数足以应付这些问题 了,
- 效果图如下:Demo演示地址:点击这里主要的JS代码如下:var tbody = document.querySelector('#
- 前言go mod tidy的作用是把项目所需要的依赖添加到go.mod,并删除go.mod中,没有被项目使用的依赖。Tidy makes s
- 没人愿意等待。所以,没有访问者真的能够忍受一个打开速度极慢的网站。但是,网页打开速度到底对用户行为有什么影响,恐怕没几个人能够说清楚吧。前几
- vue在data中定义图片相对路径:data() { return { active: 1, ico
- 测试1deco运行,但myfunc并没有运行def deco(func): print 'bef
- 本例使用登录页面演示,session的状态保持功能。说明:因为http是无状态的,客户端请求一次页面后,就结束了,当再次访问时,服务器端并不
- Pytorch转ONNX的意义一般来说转ONNX只是一个手段,在之后得到ONNX模型后还需要再将它做转换,比如转换到TensorRT上完成部
- 本文实例讲述了Python实现的knn算法。分享给大家供大家参考,具体如下:代码参考机器学习实战那本书:机器学习实战 (Peter Harr
- 在python的使用中,有时也不得不调用一下外部程序,那么如何调用外部程序:首先,我们要启动python软件,使用的是python2.7的版
- 如下所示:#python解决字符串倒序输出def string_reverse(m): num=len(m) a=[] for i in r
- 这篇文章主要介绍了python爬虫 批量下载zabbix文档代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- Perl对文件的操作,跟其它的语言类似,无非也就是打开,读与写的操作。1. 打开文件#! c:/perl/bin/perl -w use u
- 在使用ros的时候经常会用到rosbag来录制或者回放算法,是个非常有用的工具。rosbag 命令列表命令作用record录制一个包,并且指
- 1.功能简介此程序模拟用户登陆商城后购买商品操作。可实现用户登陆、商品购买、历史消费记查询、余额和消费信息更新等功能。首次登陆输入初始账户资
- 1. 使用默认的session, 在ini文件中:from pyramid.session import UnencryptedCookie
- 本文实例讲述了js实现带有介绍的Select列表菜单。分享给大家供大家参考。具体如下:带有介绍的Select列表菜单特效代码,并不是导航菜单
- 数据读取与保存Text文件对于 Text文件的读取和保存 ,其语法和实现是最简单的,因此我只是简单叙述一下这部分相关知识点,大家可以结合de
- 本文介绍了Scrapy项目实战之爬取某社区用户详情,分享给大家,具有如下:get_cookies.pyfrom selenium impor