uniapp 购物车案例

这是一个商城类项目基本必带的模块——购物车

此案例包含单选和全选的交互、数量的修改、总价的联动。

数据都是静态的数据,样式根据个人需要修改。

页面如下:
在这里插入图片描述

html代码:

	<view class="cart">
		<view class="content">
			<view class="list" v-for="item in list" :key="item.id">
				<view class="left">
					<checkbox-group @change="checkboxChange(item, item.id)"><checkbox :value="item.id" :checked="item.isChecked" /></checkbox-group>
					<image :src="item.img" class="img"></image>
				</view>
				<view class="center">
					<view class="name">{{ item.name }}</view>
					<view class="size">规格:{{ item.sku }}</view>
					<view class="count">数量:x{{ item.count }}</view>
				</view>
				<view class="right">
					<view class="price">单价¥{{ item.price }}元</view>
					<view class="update-count">
						<view class="reduce" @tap="editNum(item.id, -1)">-</view>
						<view class="cart-count">{{ item.count }}</view>
						<view class="add" @tap="editNum(item.id, 1)">+</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部导航栏 -->
		<view class="tabbar">
			<view class="all">
				<checkbox-group @change="checkboxChangeAll">
					<checkbox :checked="isAllChecked" />
					全选
				</checkbox-group>
			</view>
			<view class="totalPrice">总价:¥{{ totalPrice }}元</view>
			<view class="submitOrder" @tap="submitOrder">付款</view>
		</view>
	</view>

js代码:

<script>
export default {
	data() {
		return {
			list: [
				{
					id: '0',
					name: '商品1',
					price: 10,
					count: 1,
					sku: 'aa:bb',
					img: '/static/logo.png'
				},
				{
					id: '1',
					name: '商品2',
					price: 15,
					count: 1,
					sku: 'aa:cc',
					img: '/static/logo.png'
				},
				{
					id: '2',
					name: '商品3',
					price: 12,
					count: 1,
					sku: 'aa:dd',
					img: '/static/logo.png'
				},
				{
					id: '3',
					name: '商品4',
					price: 30,
					count: 1,
					sku: 'aa:ee',
					img: '/static/logo.png'
				}
			], //列表数据
			isAllChecked: false, //是否全选
			totalPrice: 0
		};
	},
	methods: {
		// 每个商品点击
		checkboxChange(e, id) {
			this.list[id].isChecked = !this.list[id].isChecked
			// 如果每个商品都被选中,全选勾选
			var isCheck = true
			this.list.forEach(item => {
				if(!item.isChecked){
					isCheck = false
				}
			})
			this.isAllChecked = isCheck
			// 修改选中商品重新计算价格
			this.calcPrice()
		},
		// 修改数量
		editNum(id, type) {
			if (type == 1) {
				this.list[id].count++;
			} else {
				if (this.list[id].count > 0) {
					this.list[id].count--;
				}
			}
			// 修改数量重新计算价格
			this.calcPrice()
		},
		// 计算总价
		calcPrice(){
			var totalPrice = 0
			for (let i = 0; i < this.list.length; i++) {
				// 只计算选中的商品
				if (this.list[i].isChecked) {
					totalPrice += Number((Number(this.list[i].count) * Number(this.list[i].price) * 100 / 100).toFixed(2))
				}
			}
			this.totalPrice = totalPrice
		},
		// 底部全选
		checkboxChangeAll() {
			// 更改全选状态
			this.isAllChecked = !this.isAllChecked;
			// 根据全选状态对每个商品进行更改
			if (this.isAllChecked) {
				this.list = this.list.map(item => {
					item.isChecked = true;
					return item;
				});
			} else {
				this.list = this.list.map(item => {
					item.isChecked = false;
					return item;
				});
			}
			this.calcPrice()
		},
		// 付款
		submitOrder() {
			var arr = [];
			var name = ''
			var id = ''
			if (this.list.length > 0) {
				for (let i = 0; i < this.list.length; i++) {
					if (this.list[i].isChecked) {
						arr.push(this.list[i])
						name += this.list[i].name + ','
						id += this.list[i].id + ','
					}
				}
			}
			if(arr.length == 0){
				uni.showToast({
					icon:'none',
					title:'你得选商品啊',
					duration:1500
				})
			} else {
				console.log(arr, '选中商品的数组')
				console.log(name.substring(0,name.length - 1), '选中商品的名字')
				console.log(id.substring(0,id.length - 1), '选中商品的id')
			}
		}
	},
	mounted() {
		// 一般情况,是否选中这个值后台是不返回的,所以需要在每一项加上是否选中的标志 根据具体情况选择加不加这个操作
		this.list = this.list.map(item => {
			item.isChecked = false;
			return item;
		});
	}
};
</script>

css样式:

<style lang="scss" scoped>
.content {
	width: 670rpx;
	margin: 0 auto 180rpx;
}

// 居中显示
@mixin textCenter {
	display: flex;
	align-items: center;
	justify-content: center;
}

.list {
	width: 672rpx;
	height: 208rpx;
	background: #f9f9f9;
	box-shadow: 0 8rpx 16rpx 0 rgba(83, 66, 49, 0.08);
	border-radius: 24rpx;
	margin-top: 32rpx;
	display: flex;
	justify-content: space-around;
	align-items: center;

	.left {
		display: flex;
		align-items: center;

		.img {
			width: 136rpx;
			height: 136rpx;
			margin-left: 10rpx;
			border-radius: 8%;
		}
	}

	.center {
		width: 170rpx;

		.name {
			font-size: 26rpx;
			color: #3e3e3e;
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
		}

		.size,
		.count {
			font-size: 22rpx;
			color: #8d8d8d;
		}
	}

	.right {
		.price {
			margin-top: 40rpx;
			font-size: 26rpx;
			margin-left: 40rpx;
		}

		// 加减数量
		.update-count {
			margin-top: 40rpx;
			display: flex;

			.reduce,
			.add {
				width: 40rpx;
				height: 40rpx;
				background: #f1ece7;
				border-radius: 21.6rpx;
				color: #979797;
				@include textCenter;
				font-size: 50rpx;
			}

			.cart-count {
				width: 72rpx;
				height: 40rpx;
				line-height: 40rpx;
				background: #f1ece7;
				border-radius: 21.6rpx;
				margin-left: 18rpx;
				margin-right: 18rpx;
				text-align: center;
			}
		}
	}
}

// 底部导航
.tabbar {
	width: 100%;
	height: 176rpx;
	background-color: #f3f3f3;
	position: fixed;
	bottom: 0;
	display: flex;
	align-items: center;
	justify-content: space-around;
	border-radius: 8% 8%;

	.all {
		font-size: 32rpx;
		color: #3e3e3e;
		letter-spacing: 2.29rpx;
		display: flex;
	}

	.totalPrice {
		font-size: 32rpx;
		color: #3e3e3e;
		letter-spacing: 2.29rpx;
		color: red;
	}

	.submitOrder {
		width: 208rpx;
		height: 80rpx;
		background: #354e44;
		border-radius: 14rpx;
		border-radius: 14rpx;
		font-size: 36rpx;
		color: #ffffff;
		letter-spacing: 2.57rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
}
</style>

当然还有优化空间,简单记录一下。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些uni-app页面样式模板,你可以根据需要进行调整和修改: 1. 底部Tab栏样式 ``` <template> <view class="container"> <view class="content"> <!-- 页面内容 --> </view> <view class="tabbar"> <view class="tabbar-item" :class="{'active': activeIndex === 0}" @click="activeIndex = 0"> <text>首页</text> </view> <view class="tabbar-item" :class="{'active': activeIndex === 1}" @click="activeIndex = 1"> <text>分类</text> </view> <view class="tabbar-item" :class="{'active': activeIndex === 2}" @click="activeIndex = 2"> <text>购物车</text> </view> <view class="tabbar-item" :class="{'active': activeIndex === 3}" @click="activeIndex = 3"> <text>我的</text> </view> </view> </view> </template> <script> export default { data() { return { activeIndex: 0 } } } </script> <style> .container { height: 100%; display: flex; flex-direction: column; } .content { flex: 1; } .tabbar { display: flex; height: 50px; background-color: #fff; border-top: 1px solid #ccc; } .tabbar-item { flex: 1; display: flex; justify-content: center; align-items: center; color: #666; } .tabbar-item.active { color: #f00; } </style> ``` 2. 商品列表样式 ``` <template> <view class="container"> <view class="list"> <view class="item" v-for="(item, index) in goodsList" :key="index"> <image class="item-img" :src="item.imgUrl"></image> <view class="item-info"> <text class="item-name">{{item.name}}</text> <text class="item-price">{{item.price}}</text> <button class="item-btn" @click="addToCart(item)">加入购物车</button> </view> </view> </view> </view> </template> <script> export default { data() { return { goodsList: [ {id: 1, name: '商品1', price: '10元', imgUrl: 'xxx'}, {id: 2, name: '商品2', price: '20元', imgUrl: 'xxx'}, {id: 3, name: '商品3', price: '30元', imgUrl: 'xxx'} ] } }, methods: { addToCart(item) { // 加入购物车逻辑 } } } </script> <style> .container { padding: 10px; } .list { display: flex; flex-wrap: wrap; justify-content: space-between; } .item { width: 48%; display: flex; flex-direction: column; align-items: center; margin-bottom: 10px; background-color: #fff; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 5px #ccc; } .item-img { width: 100%; height: 150px; border-radius: 5px 5px 0 0; } .item-info { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 10px; text-align: center; } .item-name { font-size: 14px; margin-bottom: 5px; } .item-price { font-size: 16px; color: #f00; margin-bottom: 5px; } .item-btn { width: 80%; height: 30px; background-color: #f00; color: #fff; border: none; border-radius: 5px; margin-top: 10px; } </style> ``` 以上仅是示例样式模板,你可以根据实际需求进行修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值