移动端购物车全选反选
使用的是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>
注:样式可能会有些误差,需要自己调整一下