用Vue购物功能实现

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
		<script type="text/javascript">
			window.onload = function(){
				new Vue({
					el:'#app',
					data:{
						students:[
							{sid:1,name:'Apple',price:10,amount:1,del:'删除'},
							{sid:2,name:'Orange',price:5,amount:1,del:'删除'},
							{sid:3,name:'Banana',price:20,amount:1,del:'删除'}
						]
					},
					methods:{
						del:function(index){
							this.students.splice(index,1);
						}
					},
					computed:{
						totalAge:function(){
							var sum = 0;
							for (var i = 0; i < this.students.length; i++) {
								sum += this.students[i].price*this.students[i].amount;
							}
							return sum;
						}
					}
				})
			}
			
		</script>
	</head>
	<body>
		
		<div id="app">
			<table class="table table-striped table-hover table-light">
				<tr>
					<th colspan="2">商品名称</th>
					<th>商品单价</th>
					<th>购买数量</th>
					<th>操作</th>
				</tr>
				<tr v-for="(student,i) in students">
					<td>{{student.sid}}</td>
					<td>{{student.name}}</td>
					<td>{{student.price*student.amount}}</td>
					<td><button @click="student.amount++"></button>{{student.amount<=0?student.amount=0:student.amount}}<button @click="student.amount--"></button></td>
					<td @click="del()">{{student.del}}</td>
				</tr>
				<tr><td colspan="5">总价:{{totalAge}}</td></tr>
			</table>
			
		</div>
		
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值