vuejs实现购物车功能

<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>

转载于:https://www.cnblogs.com/yanyufusu/p/7875279.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值