使用vue制作简单购物车实现添加修改删除

 vue实现简单购物车功能。

根据增加商品数量、减少商品数量、移除商品动态更新总数以及总价格。

可以对购物车进行编辑、添加、删除等功能。

 

话不多说直接上代码:

不过首先要引入vue,不想用本地这个可以直接复制粘贴

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

html部分

<div id="app">

			<table border="1">
				<tr>
					<th>名称</th>
					<th>价格</th>
					<th>数量</th>
					<th>操作</th>
				</tr>
				<tr :style="item.color" v-for="(item,index) in list">
					<td>{{item.name}}</td>
					<td>¥{{item.price}}</td>
					<td><button @click="decrease(index)"
							v-show="list[index].quantity !=0 ">-</button>{{item.quantity}}<button
							@click="increase(index)">+</button></td>
					<td>
						<button @click="click_edit(index)">编辑</button>&nbsp;
						<button @click="del(index)">删除</button>
					</td>
				</tr>

				<tr>
					<td></td>
					<td>总价¥:{{Price}}</td>
					<td>数量:{{Quantity}}</td>
					<td></td>
				</tr>
			</table>

			<!-- 添加 -->
			<button @click="add_to(index)" v-show = "addt==0">添加商品</button>
			<div v-show="add == 0">
				
				<button @click="determine()">添加</button>&nbsp;<button @click="remove(index)">取消</button>
				商品名称<input type="text" v-model="input_name">
			    商品价格<input type="number" v-model="input_price">
			</div>
			<!-- 修改 -->
			<div v-show="edit == 0 ">
				商品名称<input type="text" v-model="input_name">
				商品价格<input type="number" v-model="input_price"><br>
				<button @click="preservation(index)">保存</button>&nbsp;
				<button @click="cancel(index)">取消</button>
			</div>
		</div>

 js部分

	<script>
			Vue.createApp({
				data() {
					return {
						add: 1, //是否点击添加赋值状态
						edit: 1, //是否点击编辑赋值状态
						addt:0,//添加商品是否隐藏
						input_name: "",
						input_price: "",
						red: "123456",
						list: [{
							name: "商品1",
							price: 11,
							quantity: 0,
							color: {
								background: ""
							},
						}, {
							name: "商品2",
							price: 12,
							quantity: 0,
							color: {
								background: ""
							},
						}]

					}

				},
				computed: { //计算属性
					// 总价
					Price() {
						let sum = 0;
						for (var i = 0; i < this.list.length; i++) {
							sum += this.list[i].price * this.list[i].quantity;

						};
						return sum;

					},
					Quantity() {
						let sum = 0;
						for (var i = 0; i < this.list.length; i++) {
							sum += this.list[i].quantity;

						};
						return sum;

					}
				},
				methods: {

					//事件属性
					//删除 -
					decrease(index) {
						this.list[index].quantity--;
						if (this.list[index].quantity == 0) {
							this.list[index].color.background = "";
						}
					},
					// 添加 +
					increase(index) {
						this.list[index].quantity++;
						if (this.list[index].quantity > 0) {
							this.list[index].color.background = "rgba(255, 255, 0, 0.1)";
						}



					},
					// 点击编辑
					click_edit(index) {
						this.addt = 1;
						this.edit = 0;
						this.add=1;
						this.input_name = this.list[index].name;
						this.input_price = this.list[index].price;
						this.index = index; // 全局index

					},
					// 点击保存
					preservation(index) {
						this.list[index].name = this.input_name;
						this.list[index].price = this.input_price;
						this.addt=0;
						this.edit = 1;
					},
					// 点击取消
					cancel(index) {
						this.edit = 1;
						this.addt=0;

					},
					// 点击删除
					del(index) {
						this.list.splice(index, 1)
					},
					// 点击添加
					add_to(index) {
						this.add = 0;
						this.addt=1;
						this.input_name = '';
						this.input_price = '';
					},
					// 确认添加
					determine() {
						this.list.push({
								name: this.input_name,
								price: this.input_price,
								quantity: 0,
								color: {
									background: ""
								},
							}),
							this.add = 1;
							this.addt = 0;
						this.input_name = '';
						this.input_price = '';
					},
					//取消添加
					remove() {
						this.add = 1;
						this.addt = 0;
					}


				}
			}).mount('#app')
		</script>

css

<style>
		*{
			margin: 0%;
			padding: 0%;
		}
		#app {
			position: absolute;
			left: 33%;
			top: 25%;
			
			
		}
		
		table {
			width: 500px;
			height: 300px;
			text-align: center;
		}
	</style>

到这里就结束了。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单Vue购物车示例代码。假设您已经有了商品列表和每个商品的价格,您需要实现以下功能: 1. 点击“加入购物车”按钮时,将商品添加购物车中。 2. 显示购物车中的商品数量和总价格。 3. 可以从购物车删除商品。 下面是一个简单Vue购物车示例代码: ```html <!DOCTYPE html> <html> <head> <title>Shopping Cart</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app"> <h2>商品列表</h2> <ul> <li v-for="(item, index) in items" :key="index"> {{ item.name }} - {{ item.price }} <button @click="addToCart(index)">加入购物车</button> </li> </ul> <h2>购物车</h2> <p>数量:{{ cartItems.length }}</p> <p>总价:{{ getTotalPrice() }}</p> <ul> <li v-for="(item, index) in cartItems" :key="index"> {{ item.name }} - {{ item.price }} <button @click="removeFromCart(index)">删除</button> </li> </ul> </div> <script> var app = new Vue({ el: '#app', data: { items: [ {name: '商品1', price: 10}, {name: '商品2', price: 20}, {name: '商品3', price: 30}, {name: '商品4', price: 40}, {name: '商品5', price: 50}, ], cartItems: [], }, methods: { addToCart: function(index) { this.cartItems.push(this.items[index]); }, removeFromCart: function(index) { this.cartItems.splice(index, 1); }, getTotalPrice: function() { var totalPrice = 0; for (var i = 0; i < this.cartItems.length; i++) { totalPrice += this.cartItems[i].price; } return totalPrice; }, }, }); </script> </body> </html> ``` 在这个示例中,我们首先声明了一个Vue实例,并定义了两个数据属性:`items`和`cartItems`。`items`是商品列表,包含每个商品的名称和价格。`cartItems`是购物车列表,包含当前加入购物车的商品。 在`addToCart`方法中,我们将所选商品添加购物车列表中。在`removeFromCart`方法中,我们可以从购物车删除某个商品。在`getTotalPrice`方法中,我们计算购物车中所有商品的总价格。 在HTML中,我们使用Vue的指令`v-for`对商品列表和购物车列表进行循环,并使用`v-bind`将商品的名称和价格绑定到HTML元素上。我们还使用`v-on`指令将点击事件绑定到“加入购物车”按钮和“删除”按钮上,以便在单击按钮时触发相应的方法。最后,我们使用Vue的双向数据绑定将购物车中商品数量和总价绑定到HTML元素上,以便实时更新。 这是一个非常基本的Vue购物车示例,您可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值