Vue实现购物车的基本功能

Vue实现购物车商品 加、减、单选、全选、删除、价格更新等功能

在这里插入图片描述
在这里插入图片描述

Dome和Vue代码

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>商城</title>
		<link rel="stylesheet" href="./css/common.css">
		<link rel="stylesheet" href="./css/cart.css">
	</head>
	<body>
		<div id="main">
			<div class="container">
				<div id="cart">
					<h1>购物车</h1>
					<form action="#" method="post">
						<table class="form">
							<thead>
								<tr>
									<th width="8%">选择</th>
									<th width="50%">商品</th>
									<th width="13%">单价(元)</th>
									<th width="15%">数量</th>
									<th width="14%">金额(元)</th>
								</tr>
							</thead>
							<tbody id="cart-goods-list">
								<tr v-for="cart in productList">
									<td>
										<input type="checkbox" name="good-id" :value="1" v-model="cart.select">
									</td>
									<td class="goods">
										<div class="goods-image">
											<img v-bind:src="cart.pro_img">
										</div>
										<div class="goods-information">
											<h3>{{cart.pro_name}}</h3>
											<ul>
												<li>{{cart.pro_purity}}</li>
												<li>{{cart.pro_service}}</li>
											</ul>
										</div>
									</td>
									<td>
										<span class="price"><em class="price-em">{{cart.pro_price.toFixed(2)}}</em></span>
									</td>
									<td>
										<div class="combo">
											<input type="button" name="minus" value="-" class="combo-minus" @click="cart.pro_num<2?cart.pro_num=1:cart.pro_num--">
											<input type="text" name="count" v-model.number="cart.pro_num" class="combo-value">
											<input type="button" name="plus" value="+" class="combo-plus" v-on:click="cart.pro_num++">
										</div>
									</td>
									<td>
										<strong class="amount"><em class="amount-em">{{(cart.pro_price*cart.pro_num).toFixed(2)}}</em></strong>
									</td>
								</tr>
							</tbody>
							<tfoot v-show="productList.length!=0">
								<tr>
									<td colspan="2">
										<label>
                                            <input type="checkbox" name="all" v-model="isSelectAll">
                                            <span @click="">全选</span>
                                        </label>
										<a href="#" id="cart-delete" @click="del()">删除</a>
									</td>
									<td colspan="3">
										<span>合计:</span>
										<strong id="total-amount"><em id="total-amount-em">{{getTotal}}</em></strong>
										<input type="submit" value="立即结算" id="settlement">
									</td>
								</tr>
							</tfoot>

						</table>
					</form>
					<div v-show="productList.length===0">
						购物车还是空的哦~快来购物吧~
					</div>
				</div>
			</div>
		</div>
	</body>
	<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		new Vue({
			el:"#cart",
			data:{
				productList:[
						{
						'pro_name': 'Dior 迪奥 花漾甜心小姐 女士淡香水',//产品名称
						'pro_purity': '50ml',//规格
						'pro_service': "不支持7天无理由退货",//售后
						'pro_num': 1,//数量
						'pro_img': 'img/1.jpg',//图片链接
						'pro_price': 498,//单价,
						'select': true ,//选中状态
					},
					{
						'pro_name': '迪奥(dior)口红CD烈艳蓝金唇膏',//产品名称
						'pro_purity': '350g',//规格
						'pro_service': "不支持7天无理由退货",//售后
						'pro_num': 1,//数量
						'pro_img': 'img/2.jpg',//图片链接
						'pro_price': 268,//单价
						'select': true //选中状态
					},
					{
						'pro_name': 'LANCÔME 兰蔻 嫩肌活肤精华肌底液',//产品名称
						'pro_purity': '50ml',//规格
						'pro_service': "不支持7天无理由退货",//售后
						'pro_num': 1,//数量
						'pro_img': 'img/3.jpg',//图片链接
						'pro_price': 598,//单价
						'select': true //选中状态
					}
				]
			},
			computed:{
				getTotal:function(){
					var newArr=this.productList.filter(function(val){
						return val.select===true;
					})
					var price=0;
					for(var i=0;i<newArr.length;i++){
						price+=newArr[i].pro_num*newArr[i].pro_price
					}
					return price.toFixed(2)
				},
				isSelectAll:{
					get:function(){
						return this.productList.every(function(val){
							return val.select===true;
						})
					},
					set:function(newValue){
						for(var i=0;i<this.productList.length;i++){
							this.productList[i].select=newValue;
						}
					}
				}
			},
			methods:{
				del:function(){
					if(confirm("确定要删除吗")){
						var newArr=[];
						for(var i=0;i<this.productList.length;i++){
							if(this.productList[i].select===false){
								newArr.push(this.productList[i])
							}
						}
						this.productList=newArr;
					}
				}
			}
			
		})
	</script>
</html>

购物车部分CSS代码

@charset "utf-8";

#main{
    padding: 30px 0px;
}

#cart{
    background: #FFFFFF;
    padding: 40px;
}

#cart h1{
    line-height: 40px;
    padding: 0px 0px 10px 0px;
}

table.form{
    border-collapse: collapse;
    empty-cells: show;
    margin: 20px 0px;
    padding: 0px;
    table-layout: fixed;
    width: 100%;
}

table.form th,
table.form td{
    border-bottom: 1px solid #DDDDDD;
    padding: 15px 10px;
    text-align: left;
}

table.form{
    border-top: 3px solid #DDDDDD;
}

.goods .goods-image img{
    border: 1px solid #DDDDDD;
    float: left;
    height: 100px;
    margin: 0px 20px 0px 0px;
}

.goods .goods-information{
    float: left;
}

.goods .goods-information ul{
    color: #666666;
    font-size: 12px;
    line-height: 20px;
    margin:10px 0px 0px 0px;
}

.price,
.amount,
#total-amount{
    color: #E00000;
}

#total-amount{
    font-size: 22px;
}

.price em,
.amount em,
#total-amount em{
    font-style: normal;
}

.combo .combo-minus,
.combo .combo-value,
.combo .combo-plus{
    background: #FFFFFF;
    border: 1px solid #DDDDDD;
    color: #333333;
    float: left;
    font-weight: bold;
    margin: 0px;
    outline: none;
    text-align: center;
}

.combo .combo-minus,
.combo .combo-plus{
    font-size: 16px;
    height: 26px;
    line-height: 26px;
    padding: 0px;
    width: 24px;
}

.combo .combo-value{
    border-left: none;
    border-right: none;
    height: 20px;
    line-height: 20px;
    padding: 2px;
    width: 40px;
}

#cart-delete{
    margin-left: 20px;
}

#settlement{
    background: #E00000;
    border: none;
    color: #FFFFFF;
    float: right;
    font-size: 16px;
    height: 40px;
    line-height: 40px;
    margin: 0px;
    outline: none;
    padding: 0px;
    width: 160px;
}

注:CSS样式代码由于太多上面没有给全,只给了主要代码。小伙伴们可以根据实际情况修改样式~~~

  • 22
    点赞
  • 145
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
实现购物车功能需要结合Spring Boot和Vue框架,以下是一种可能的实现方案。 首先,在Spring Boot中创建商品和购物车相关的数据模型和数据库表结构。可以创建商品表、购物车表,以及中间表来建立商品和购物车之间的关系。通过JPA或者MyBatis等框架与数据库进行交互,实现增删改查等基本的数据操作。 接下来,使用Spring Boot创建商品和购物车相关的接口。可以创建商品的添加、删除、修改等接口,以及购物车的商品添加、删除、修改接口。在接口中,可以处理一些逻辑,比如商品添加到购物车时可以判断购物车是否已存在该商品,如果存在则数量增加,不存在则添加新的购物车项。 在Vue中,创建商品列表和购物车页面。可以使用axios等工具从后端获取商品列表,并展示给用户。用户在页面上可以点击“加入购物车”按钮,将选中的商品添加到购物车中。在购物车页面,展示当前用户已添加的购物车商品列表,并实现数量增减和删除的功能。每次数量改变或删除操作时,通过接口与后端进行数据交互,更新购物车中的商品信息。 在Vue中,还需要实现购物车商品总价的计算和结算功能。可以通过遍历购物车商品列表,计算每个商品的小计,再将所有小计相加得到总价。用户点击结算按钮时,可以将购物车商品列表传递给后端进行支付操作。 以上是基本的购物车功能实现方案。当然,具体实现过程中可能还会涉及到其他细节问题,比如商品详情展示、用户登录、权限控制等,需要根据实际需求进一步完善和实现
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值