vue实现购物车全选反选功能的方法是什么?
Admin 2021-10-29 群英技术资讯 1281 次浏览
这篇文章给大家分享的是vue实现购物车全选反选功能的方法。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
这是效果图:

话不多少,直接上代码:
<template>
<div class="cart">
<div class="st-spacing-main" v-for="(item) in cartInfoList" :key="item.id">
<div class="st-item product-item">
<div class="st-border-bottom store-title">
<p @click="checkShop(item)">
<van-checkbox v-model="item.checked">
<span>
{{item.shopTitle}}
<van-icon name="arrow"/>
</span>
</van-checkbox>
</p>
</div>
<ul class="commodity-list" v-for="(pros,value) in item.productList" :key="value">
<li @click="ischeck(item,pros)">
<van-checkbox v-model="pros.isChecked"></van-checkbox>
</li>
<li>
//这是商品组件
<product-cart size="mini" type="number" :option="3"></product-cart>
</li>
</ul>
</div>
</div>
<van-submit-bar class="settlement" :price="10060" button-text="去结算" @submit="onSubmit">
<van-checkbox v-model="isCheckAll" @click="checkAll()">全选</van-checkbox>
<span class="cart-freight" slot="default">不含运费</span>
</van-submit-bar>
</div>
</template>
<script>
export default {
data () {
return {
cartInfoList: [
{
id: 1,
shopTitle: '苹果旗舰店', // 商店名
checked: false, // 商店选择的状态
checkedCount: 0, // 此商店被选择的商品数量
productList: [
{
isChecked: false, // 商品选择状态
productTitle: '2019款macbook/苹果/MF893/A国航笔记本', // 产品名
category: '15寸/2.3/8G/256/土豪金/标准套餐',
price: 10200, // 价格
count: 1 // 数量
}
]
},
{
id: 2,
shopTitle: '锤子科技旗舰店',
checked: false,
checkedCount: 0,
productList: [
{
isChecked: false,
productTitle: '锤子手机手感保护膜',
category: '登陆月球',
price: 9.9,
count: 1
},
{
isChecked: false,
productTitle: '锤子手机pro割手版',
category: '128G/割手版',
price: 1790,
count: 1
}
]
}
],
isCheckAll: false, // 是否全选
allPrice: 0, // 所有价格
allShops: 0, // 被选中的商店数量
allCount: 0 // 被选中的产品数量
}
},
methods: {
// 选中单个商品
ischeck (item, pro) {
// 为未选中的时候改变为false,反之为true
!pro.isChecked ? this._checkTrue(item, pro) : this._checkFalse(item, pro)
},
// 修改单个商品的true
_checkTrue (item, pro) {
pro.isChecked = true // 将商品选中状态改为true
// 这里执行了两部,选中商品数量先+1,再与该店铺商品数量比较,如果相等就更改店铺选中状态为true
if (++item.checkedCount === item.productList.length) {
item.checked = true
} else {
return ''
}
// ++item.checkedCount === item.productList.length ? item.checked = true : ''
// 如果店铺选中状态改为true,选中店铺数量先+1,再与店铺数量比较,如果相等就更改全选选中状态为true
// // 当商店全选状态,每全选一个商店,被选中商店数加一,数值等于所有商店数,全选状态为true
if (item.checked) {
if (++this.allShops === this.cartInfoList.length) {
this.isCheckAll = true
} else {
this.isCheckAll = false
}
} else {
return ''
}
// item.checked ? ++this.allShops === this.cartInfoList.length ? this.isCheckAll = true : this.isCheckAll = false : ''
},
// 修改单个商品的 false
_checkFalse (item, pro) {
pro.isChecked = false // 将商品选中状态改为false
--item.checkedCount // 被选中的商品数减一
if (item.checked) {
// 如果店铺是被选中的,更改店铺选中状态
item.checked = false // 当商店状态为选中时改变false
--this.allShops // 商店数减一
}
this.isCheckAll = false // 全选状态为false
},
// 选择整个商店的商品
checkShop (item) {
!item.checked ? this._shopTrue(item) : this._shopFalse(item)
},
// 遍历商店每个商品,状态为false的改变为true,又在_checkTrue()方法中将商店状态改为true
_shopTrue (item) {
item.productList.forEach(pro => {
if (pro.isChecked === false) {
this._checkTrue(item, pro)
} else {
return ''
}
// pro.isChecked === false ? this._checkTrue(item, pro) : ''
})
},
_shopFalse (item) {
item.productList.forEach(pro => {
// 同上
if (pro.isChecked === true) {
this._checkFalse(item, pro)
} else {
return ''
}
// pro.isChecked === true ? this._checkFalse(item, pro) : ''
})
},
// 选择全部商店的商品,已经计算总价和总数量的函数
checkAll () {
// 方法内调用方法
this.isCheckAll = !this.isCheckAll
this.isCheckAll
? this.cartInfoList.forEach(item => {
this._shopTrue(item)
})
: this.cartInfoList.forEach(item => {
this._shopFalse(item)
})
},
_totalPeice () {
// 每次调用此方法,将初始值为0,遍历价格并累加
this.allPrice = 0
this.cartInfoList.forEach(item => {
let products = item.productList
products.forEach(pros => {
if (pros.isChecked) {
this.allPrice += pros.price * pros.count
}
})
})
},
_totalCount () {
// 同上
this.allCount = 0
this.cartInfoList.forEach(item => {
this.allCount += item.checkedCount
})
},
onSubmit () {}
},
watch: {
// 深度监听所有数据,每次改变重新计算总价和总数
cartInfoList: {
deep: true,
handler (val, oldval) {
this._totalPeice()
this._totalCount()
}
}
}
}
</script>
<style lang="less" scoped>
@import "../assets/less/views/cart.less";
</style>
less文件
@spacing-size: .29rem;
.cart-main {
.st-item-header {
padding: @spacing-size;
justify-content: flex-start;
.theme-checkbox {
margin-right: @spacing-size;
}
}
.item-list {
padding-left: .77rem;
position: relative;
.theme-checkbox {
margin-top: -.24rem;
position: absolute;
left: 0;
top: 50%;
}
}
}
以上就是关于vue实现购物车全选反选功能的介绍了,本文只是提供了一种实现思路,代码仅供参考,有需要的朋友可以了解看看,希望对大家学习vue框架有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
我们在登录或者注册网页账号的时候,往往需要输入验证码,那么验证码是如何实现的呢?下面就给大家分享一下关于使用JS实现验证码的内容,包括普通的验证码和带干扰项的动态验证码,效果如下图所示,感兴趣的朋友可以看看。
目录1.ES5常用:利用for嵌套for,然后splice去重2.ES6常用:Set去重3.indexOf去重4.sort()排序5.对象属性不能相同(不建议)6.includes()7.hasOwnProperty8.filter9.利用递归去重10.Map去重11.reduce+includes1.ES5常用:利用
windows下安装Node出现报错2503怎么解决?有一些朋友就有遇到node安装失败2503报错的问题,那么究竟我们该怎么办呢?下文给大家分享了一个解决方法,操作如下,大家可以了解看看,希望能帮助到大家。
这篇文章主要介绍了vue 图片裁剪上传组件的实现,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下
选择器表示要定义样式的对象,可以是元素本身,也可以是一类元素或者制定名称的元素。常用的选择器有十种左右类型选择符,id选择器,class选择器,通配符等。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008