vue实现购物车增删改

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.upBox{
				width:300px;
				height:300px;
				position: absolute;
				left: 0;top: 0;bottom: 0;right: 0;
				margin: auto;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<div>
				编号:<input type="text" v-model="id" /><br>
				书名:<input type="text" v-model="name" /><br>
				作者:<input type="text" v-model="author" /><br>
				价格:<input type="text" v-model="price" /><br>
				<input type="button"  value="添加" @click="add"/>
			</div>
			<div class="upBox" v-show="isShowBox">
				编号:<input type="text" v-model="upid" /><br>
				书名:<input type="text" v-model="upname" /><br>
				作者:<input type="text" v-model="upauthor" /><br>
				价格:<input type="text" v-model="upprice" /><br>
				<input type="button"  value="添加" @click="addBook"/>
				<input type="button" value="取消" @click="cancel"/>
			</div>
			<hr >
			<table>
				<tr>
					<td>编号</td>
					<td>书名</td>
					<td>作者</td>
					<td>价格</td>
					<td>删除</td>
					<td>修改</td>
				</tr>
				<tr v-for="(book,index) in bookObjs">
					<td>{{book.id}}</td>
					<td>{{book.name}}</td>
					<td>{{book.author}}</td>
					<td>{{book.price}}</td>
					<td><input type="button" value="删除" @click="deleteBook(index)"/></td>
					<td><input type="button" value="修改" @click="updateBook(index)"/></td>
				</tr>
			</table>
		</div>
	</body>
</html>
<script src="../02code/js/vue.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	let vm=new Vue({
		el:"#app",
		data:{
			isShowBox:false,
			upIndex:-1,
			id:"",
			name:"",
			author:"",
			price:"",
			
			upid:"",
			upname:"",
			upauthor:"",
			upprice:"",
		
			bookObjs: [
			    {
			        id: "001",
			        name: "三国演义",
			        author: "罗贯中",
			        price: 30
			    },
			    {
			        id: "002",
			        name: "西游记",
			        author: "吴承恩",
			        price: 35
			    },
			    {
			        id: "003",
			        name: "水浒传",
			        author: "施耐庵",
			        price: 45
			    }
			]
		},
		methods:{
			add(){
				this.bookObjs.push({
					id:this.id,
					name:this.name,
					author:this.author,
					price:this.price
				});
			},
			deleteBook(index){
				if(confirm("确定要删除信息?")){
					this.bookObjs.splice(index,1);
				}
			},
			updateBook(index){
				this.isShowBox=true;
				this.upIndex=index;
				// 数据变化
				this.upid=this.bookObjs[index].id;
				this.upname=this.bookObjs[index].name;
				this.upauthor=this.bookObjs[index].author;
				this.upprice=this.bookObjs[index].price;		
			},
			addBook(){
				this.isShowBox=false;
				this.bookObjs[this.upIndex].name=this.upid;
				this.bookObjs[this.upIndex].name=this.upname;
				this.bookObjs[this.upIndex].author=this.upauthor;
				this.bookObjs[this.upIndex].price=this.upprice;
			},
			cancel(){
				this.isShowBox=false;
			}
		}
	});
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Vue和Element UI可以非常方便地实现表格的增删功能。 首先,我们需要在Vue中引入Element UI的Table组件,用于显示表格数据。可以通过在Vue组件中引入以下代码来实现: ```javascript <template> <el-table :data="tableData"> <!-- 表格列的定义 --> </el-table> </template> <script> import { Table } from 'element-ui'; export default { components: { 'el-table': Table }, data() { return { tableData: [] // 表格数据 }; } } </script> ``` 接下来,我们需要在data函数中定义tableData,用于存储表格的数据。可以通过向tableData数组中添加对象的方式来定义表格的行数据。例如: ```javascript data() { return { tableData: [ { name: 'Alice', age: 20 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 30 } ] }; } ``` 然后,我们可以使用Element UI提供的Button组件来实现新增、删除和编辑操作。例如: ```javascript <el-button @click="addRow">新增</el-button> <el-button @click="deleteRow">删除</el-button> <el-button @click="editRow">编辑</el-button> ``` 在Vue组件的methods中定义对应的方法来实现表格的增删功能。例如: ```javascript methods: { // 新增一行数据 addRow() { this.tableData.push({ name: '', age: '' }); }, // 删除选中的行数据 deleteRow() { const selectedRows = this.$refs.table.selection; selectedRows.forEach(row => { const index = this.tableData.indexOf(row); this.tableData.splice(index, 1); }); }, // 编辑选中的行数据 editRow() { // 编辑操作 } } ``` 通过以上步骤,即可使用Vue和Element UI实现表格的增删功能。根据具体需求,可以进一步自定义表格列的定义、编辑操作等功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值