vue改进版购物车

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.upBox{
				width: 200px;
				height: 200px;
				background-color:lightpink;
				position: absolute;
				left: 0;right: 0;top: 0;bottom: 0;
				margin: auto;
			}
		</style>
	</head>
	<body>
		<div id="shopcar">
			<div id="">
				编号:<input type="text" value="" v-model="newBook.id"/><br>
				书名:<input type="text" value="" v-model="newBook.name"/><br>
				作者:<input type="text" value="" v-model="newBook.author"/><br>
				价格:<input type="text" value="" v-model="newBook.price"/><br>
				<input type="button" value="增加" @click="addBook"/>
				<!-- 增加商品按钮,双向绑定-->
			</div>
			<p>
				&nbsp;
			</p>
			<div class="upBox" v-show="isShow">
				编号:<input type="text" value="" v-model="upDateBook.upid"/><br>
				书名:<input type="text" value="" v-model="upDateBook.upname"/><br>
				作者:<input type="text" value="" v-model="upDateBook.upauthor"/><br>
				价格:<input type="text" value=""  v-model="upDateBook.upprice"/><br>
				<input type="button" value="保存" @click="saveMessage"/>
				<input type="button" value="取消" @click="cancel"/>
				<!-- 修改商品的弹出框,默认隐藏,需要时显示 -->
			</div>
			<!-- 主表,实现删除修改功能 -->
			<table>
				<tr>
					<td>编号</td>
					<td>书名</td>
					<td>作者</td>
					<td>价格</td>
					<td>删除</td>
					<td>操作</td>
				</tr>
				<tr v-for="(books,index) in book" :key="book.id">
					<td>{{books.id}}</td>
					<td>{{books.name}}</td>
					<td>{{books.author}}</td>
					<td>{{books.price}}</td>
					<td><input type="button" value="删除" @click="delBook(index)"/></td>
					<td><input type="button" value="修改" @click="updateBook(index)"/></td>
					<!-- 主表修改删除按钮需要传参,循环出来的TR用index确定需要变动哪一行 -->
				</tr>
			</table>
		</div>
	</body>
</html>
<script type="text/javascript">
	// 1.数据渲染
	// 2.实现增加功能(双向绑定)(数组对象)(push)(清空输入框)
	// 3.删除商品(传参)
	// 4.修改商品(传参)(确认保存、取消)(显示添加单)(获取到商品信息赋值给添加单)
</script>
<script src="../01code/js/vue.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	let vm=new Vue({
		el:"#shopcar",
		data:{
			isShow:false,
			upIndex:-1,
			// 设置无效的index值
			upDateBook:{
				upid:"",
				upname:"",
				upauthor:"",
				upprice:"newBook"
			},
			newBook:{
				id:"",
				name:"",
				author:"",
				price:""
			},
			book:[
				{
					id:"001",
					name:"大学",
					author:"曾子",
					price:"80"
				},
				{
					id:"002",
					name:"中庸",
					author:"子思",
					price:"90"
				},
				{
					id:"003",
					name:"论语",
					author:"孔子",
					price:"100"
				},
			]
		},
		methods:{
			addBook(){
				this.book.push(this.newBook);
				// 清空输入框
				this.newBook={
					id:"",
					name:"",
					author:"",
					price:""
				}
			},
			delBook(index){
				this.book.splice(index,1);
			},
			updateBook(index){
				this.isShow=true;
				// 显示修改弹出框
				this.upIndex=index;
				// 保存信息时用index下标赋值
				this.upDateBook.upid=this.book[index].id;
				this.upDateBook.upname=this.book[index].name;
				this.upDateBook.upauthor=this.book[index].author;
				this.upDateBook.upprice=this.book[index].price;
				// 将主表中的数据赋值给弹出的修改框重
			},
			saveMessage(){
				this.isShow=false;
				// index为局部变量,此处无法访问
				this.book[this.upIndex].id=this.upDateBook.upid;
				this.book[this.upIndex].name=this.upDateBook.upname;
				this.book[this.upIndex].author=this.upDateBook.upauthor;
				this.book[this.upIndex].price=this.upDateBook.upprice;	
				// 点击保存按钮,隐藏修改框,将修改框中的数据赋值给主表
			},
			cancel(){
				this.isShow=false;
			}
		}
	});
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值