分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

vuejs实现购物车功能

发布时间:2023-09-06 01:26责任编辑:蔡小小关键词:jsvuejs

<body>
<h1 align="center">购物车实例</h1>
<div id="vm1">
<table align="center" width="800" border="1" cellpadding="0" cellspacing="0" >
<tr>
<th>序号</th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
<tr v-for="(product,index) in productsList">
<td>{{Number(index)+1}}</td>
<td>{{product.name}}</td>
<td class="money">{{product.price}}</td>

<td>
<button v-on:click="jian(index)">-</button>

<input type="text" v-model="product.num" v-on:input="change(index)">

<button v-on:click="add(index)">+</button>
</td>
<td class="money">{{product.price*product.num}}</td>
<td><button v-on:click="del(index)">移除购物车</button></td>
</tr>
</table>

<table align="center" width="800" border="1" cellpadding="5" cellspacing="0" style="margin-top: 60px;">
<tr>
<th>购买商品总数量</th>
<th>总价</th>
</tr>
<tr>
<td>{{ count2() }}</td>
<td class="money">{{ allMoney() }}</td>

</tr>
</table>
</div>

<script>
var vm1=new Vue({
el:‘#vm1‘,
data:{
productsList:[
{id:‘0‘,name:‘iPhone 6 plus‘,price:‘1000‘,num:1},
{id:‘1‘,name:‘电脑桌‘,price:‘300‘,num:1},
{id:‘2‘,name:‘平板‘,price:‘800‘,num:1}
]
},
methods:{
// 删除
del:function(index){
if(confirm("您确定要删除吗?")){
this.productsList.splice(index,1);
}
},
// 增加
add:function(index){
if(this.productsList[index].num<100){
this.productsList[index].num++;
}
},
// 减少
jian:function(index){
if(this.productsList[index].num>1){
this.productsList[index].num--;
}
},
// 修改
change:function(index){
if(this.productsList[index].num>100){
this.productsList[index].num=100;
}else if(this.productsList[index].num<1){
this.productsList[index].num=1;
}
},
// 总数量
count2:function(){
var List=this.productsList;
var total=0;
for(var i=0;i<List.length;i++){
total+=parseInt(List[i].num);
}
return total;
},

// 总金额
allMoney:function(){
var List=this.productsList;
var totalMoney=0;
for(var i=0;i<List.length;i++){
totalMoney+=parseInt(List[i].num)*List[i].price;
}
return totalMoney;
}


}
});


</script>
</body>

vuejs实现购物车功能

原文地址:http://www.cnblogs.com/yanyufusu/p/7875279.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved