移动端购物车全选反选

本文介绍了使用uni-app框架开发小程序时,如何实现购物车的全选和反选功能。通过遍历商品列表和维护一个选中商品数组,实现了商品的点击选择和全选按钮的状态同步。同时,展示了相关的Vue模板代码和事件处理函数,如`chooseItem`用于单个商品选择,`checkedAllItem`用于全选和取消全选操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

移动端购物车全选反选

使用的是uniapp开发小程序,其中使用了uniapp滑动操作组件

效果

在这里插入图片描述

代码

<template>
	<view class="cartList">
		<view>
			<view class="cart_list_operation">
				<view class="cart_operation_left">购物车共有 5 件商品</view>
				<view class="cart_operation_right">操作</view>
			</view>
			<view class="cart_list_content">
				<uni-swipe-action class="cart_pro_list">
				    <uni-swipe-action-item :right-options="options" v-for="(item,index) in cartGoodsList" :key="index" 
					@click="bindClick" @change="swipeChange($event, index)" class="cart_list_item">
						<view class="cart_list_item_view">
							<view :style="{'border-color':(chooseGoodsList.indexOf(item) != -1 ? '#FF9146' : '#999999')}" class="goodsChoose"  @click="chooseItem(item,index)"  >
								<icon v-if="chooseGoodsList.indexOf(item) != -1" type="success_no_circle" size="12" color="#FF9045"/>
							</view>
							{{item.id}}
						</view>
				    </uni-swipe-action-item>
				</uni-swipe-action>
			</view>
			<view class="cart_bottom_pay">
				<view class="checkAll">
					<view class="allGoodsChoose" :style="{'border-color':(allCheckedChoosed ? '#FF9146' : '#999999')}"
						@click="checkedAllItem"
					>
						<icon v-if="allCheckedChoosed" type="success_no_circle" size="12" color="#FF9045"/>
					</view>
					<text>全选</text>
				</view>
				<view class="pay_area">
					<view class="pay_area_msg">
						<view class="pay_msg_one">
							<text class="total">合计:</text>
							<view class="total_price">¥<text>0</text></view>
						</view>
					</view>
					<view class="pay_btn">去结算</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			options:[
				{
					text: '移入收藏夹',
					style: {
						width:'120upx',
						backgroundColor: '#FF9146'
					}
				}, {
					text: '删除',
					style: {
						width:'120upx',
						backgroundColor: '#FC7314'
					}
				}
			],
			count:1,
			cartGoodsList:[
				{
					id:'10001'
				},
				{
					id:'10002'
				},
				{
					id:'10003'
				},
				{
					id:'10004'
				},
				{
					id:'10005'
				}
			],
			allCheckedChoosed:false,
			chooseGoodsList:[]//选中的商品放入改列表中
		}
	},
	methods: {
		bindClick(e){
		  console.log('点击了'+(e.position === 'left' ? '左侧' : '右侧') + e.content.text + '按钮')
		},
		swipeChange(e,index){
		  console.log('当前状态:'+ e +',下标:' + index)
		},
		chooseItem(item,index){
			//商品的选择
			// 1、点击多选框可以展示出选中的样式 -- 创建个数组chooseGoodsList,并选中的商品放入数组中
			// 2、第一次点击商品是选中,再次点击是取消选中 -- 判断chooseGoodsList中是否含该商品,
			//    如果含有则删除该商品,若没有则添加
			
			if(this.chooseGoodsList.indexOf(item) !== -1){
				this.chooseGoodsList.splice(this.chooseGoodsList.indexOf(item),1)
			} else {
				this.chooseGoodsList.push(item)
			}
			
			// 3、当chooseGoodsList与goodsList相等时,全选按钮显示选中状态
			if(this.chooseGoodsList.length == this.cartGoodsList.length){
				this.allCheckedChoosed = true
			}else{
				this.allCheckedChoosed = false
			}
			
		},
		checkedAllItem(){
			// 4、点击全选时,所有商品都选中,也就是chooseGoodsList与goodsList相等
			// 5、取消全选时,所有商品都不选中,也就是chooseGoodsList为空
			if(this.allCheckedChoosed){
				this.allCheckedChoosed = false
				this.chooseGoodsList = []
			}else{
				this.chooseGoodsList = this.cartGoodsList
				this.allCheckedChoosed = true
			}
		}
	}
}
</script>
<style>
	page{
		background-color: #f8f8f8;
	}
</style>
<style scoped lang="scss">
.cartList{
	.cart_list_operation{
		width: calc(100% - 34upx);
		padding: 28upx 17upx;
		background-color: #FFFFFF;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 26upx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #333333;
		.cart_operation_right{
			padding: 0 29upx;
			border-left: 1upx solid #999999;
		}
	}
	.cart_list_content{
		margin-top: 22upx;
		.cart_pro_list{
			.cart_list_item{
				.cart_list_item_view{
					width: calc(100% - 48upx);
					height: 241upx;
					background-color: #FFFFFF;
					padding: 23upx 24upx 22upx;
					margin-bottom: 22upx;
					display: flex;
					align-items: center;
					position: relative;
					.goodsChoose{
						width:20upx;
						height: 20upx;
						border-radius: 32upx;
						border: 2upx solid #999999;
						margin-right: 16upx;
						padding: 6upx;
						margin-top: -170upx;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}
			}
		}
	}
	.cart_bottom_pay{
		width: calc(100% - 51upx);
		height: 98upx;
		background-color: #FFFFFF;
		box-shadow: 0px 1upx 0px 0px #EEEEEE;
		padding: 0 24upx 0 27upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		bottom: 0upx;
		z-index: 99;
		.checkAll{
			display: flex;
			font-size: 22upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			.allGoodsChoose{
				width:20upx;
				height: 20upx;
				border-radius: 32upx;
				border: 2upx solid #999999;
				margin-right: 16upx;
				padding: 6upx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.pay_area{
			display: flex;
			align-items: center;
			.pay_area_msg{
				display: flex;
				flex-direction: column;
				align-items: flex-end;
				.pay_msg_one{
					display: flex;
					align-items: center;
					.total{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #333333;
						margin-right: 10upx;
					}
					.total_price{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #FC7314;
						text{
							font-weight: bold;
							font-size: 36upx;
						}
					}
				}
			}
			.pay_btn{
				width: 179upx;
				height: 54upx;
				background: linear-gradient(90deg, #FFB267, #FF9045);
				border-radius: 15upx;
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				text-align: center;
				line-height: 54upx;
				margin-left: 30upx;
			}
		}
	}
}
</style>

注:样式可能会有些误差,需要自己调整一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值