Vue_简单购物车的实现

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Vue_demo2</title>
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
		<script src="../js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/bootstrap.js"></script>
		<link rel="stylesheet" href="../css/bootstrap.css" />		
		<style type="text/css">
			table,th,tr,td{
				font-size: 20px;
				text-align: center;
				border: solid 1px cornflowerblue;
				border-radius: 4px;
			}
			td,th{
				height: 50px;
				width: 120px;
			}
			.button1{
				display: inline-block;
				width: 22px;
			}
		</style>
	</head>
	<body>
		
		<div id="div1">
			<table>
				<thead>
						<th v-for="th in thList">{{th}}</th>
				</thead>
				<tbody>
					<tr v-for="obj in tdList">
						<td>{{obj.id}}</td>
						<td>{{obj.name}}</td>
						<td>
							{{obj.price}}
						</td>					
						<td>
							<button class="btn btn-warning button1" v-bind:disabled="obj.count==0" v-on:click="obj.count--">-</button><!--数量为0时不能减去-->
							{{obj.count}}
							<button class="btn btn-success button1" v-on:click="obj.count++">+</button>
						</td>
						<td><button class="btn btn-danger " v-on:click="obj.count=0" width="150px">重置</button></td>
					</tr>
				</tbody>
			</table>
			<h2>总价:{{sum()}}</h2>
		</div>
		<script type="text/javascript">
			var vm=new Vue({
				
				el:"#div1",
				data:{
					thList:["序号","名称","价格","数量","重置"],
					tdList:[{
						id:1,
						name:"华为",
						price:3000,
						count:0,
					},
					{
						id:2,
						name:"iPhone",
						price:7000,
						count:0,						
					},
					{
						id:3,
						name:"三星",
						price:6000,
						count:0,						
					}],
				},
				methods:{
					sum:function(){
						var sumPrice=0;
						for(var i=0,len=this.tdList.length;i<len;i++){
							sumPrice+=this.tdList[i].price*this.tdList[i].count;
						}
						return sumPrice;
					}
				}
				
			});
		</script>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值