vue+vant實(shí)現(xiàn)購物車全選和反選功能
本文實(shí)例為大家分享了vue+vant實(shí)現(xiàn)購物車全選和反選的具體代碼,供大家參考,具體內(nèi)容如下
這是效果圖:

話不多少,直接上代碼:
<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="去結(jié)算" @submit="onSubmit">
<van-checkbox v-model="isCheckAll" @click="checkAll()">全選</van-checkbox>
<span class="cart-freight" slot="default">不含運(yùn)費(fèi)</span>
</van-submit-bar>
</div>
</template>
<script>
export default {
data () {
return {
cartInfoList: [
{
id: 1,
shopTitle: '蘋果旗艦店', // 商店名
checked: false, // 商店選擇的狀態(tài)
checkedCount: 0, // 此商店被選擇的商品數(shù)量
productList: [
{
isChecked: false, // 商品選擇狀態(tài)
productTitle: '2019款macbook/蘋果/MF893/A國航筆記本', // 產(chǎn)品名
category: '15寸/2.3/8G/256/土豪金/標(biāo)準(zhǔn)套餐',
price: 10200, // 價(jià)格
count: 1 // 數(shù)量
}
]
},
{
id: 2,
shopTitle: '錘子科技旗艦店',
checked: false,
checkedCount: 0,
productList: [
{
isChecked: false,
productTitle: '錘子手機(jī)手感保護(hù)膜',
category: '登陸月球',
price: 9.9,
count: 1
},
{
isChecked: false,
productTitle: '錘子手機(jī)pro割手版',
category: '128G/割手版',
price: 1790,
count: 1
}
]
}
],
isCheckAll: false, // 是否全選
allPrice: 0, // 所有價(jià)格
allShops: 0, // 被選中的商店數(shù)量
allCount: 0 // 被選中的產(chǎn)品數(shù)量
}
},
methods: {
// 選中單個(gè)商品
ischeck (item, pro) {
// 為未選中的時(shí)候改變?yōu)閒alse,反之為true
!pro.isChecked ? this._checkTrue(item, pro) : this._checkFalse(item, pro)
},
// 修改單個(gè)商品的true
_checkTrue (item, pro) {
pro.isChecked = true // 將商品選中狀態(tài)改為true
// 這里執(zhí)行了兩部,選中商品數(shù)量先+1,再與該店鋪商品數(shù)量比較,如果相等就更改店鋪選中狀態(tài)為true
if (++item.checkedCount === item.productList.length) {
item.checked = true
} else {
return ''
}
// ++item.checkedCount === item.productList.length ? item.checked = true : ''
// 如果店鋪選中狀態(tài)改為true,選中店鋪數(shù)量先+1,再與店鋪數(shù)量比較,如果相等就更改全選選中狀態(tài)為true
// // 當(dāng)商店全選狀態(tài),每全選一個(gè)商店,被選中商店數(shù)加一,數(shù)值等于所有商店數(shù),全選狀態(tài)為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 : ''
},
// 修改單個(gè)商品的 false
_checkFalse (item, pro) {
pro.isChecked = false // 將商品選中狀態(tài)改為false
--item.checkedCount // 被選中的商品數(shù)減一
if (item.checked) {
// 如果店鋪是被選中的,更改店鋪選中狀態(tài)
item.checked = false // 當(dāng)商店?duì)顟B(tài)為選中時(shí)改變false
--this.allShops // 商店數(shù)減一
}
this.isCheckAll = false // 全選狀態(tài)為false
},
// 選擇整個(gè)商店的商品
checkShop (item) {
!item.checked ? this._shopTrue(item) : this._shopFalse(item)
},
// 遍歷商店每個(gè)商品,狀態(tài)為false的改變?yōu)閠rue,又在_checkTrue()方法中將商店?duì)顟B(tài)改為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) : ''
})
},
// 選擇全部商店的商品,已經(jīng)計(jì)算總價(jià)和總數(shù)量的函數(shù)
checkAll () {
// 方法內(nèi)調(diào)用方法
this.isCheckAll = !this.isCheckAll
this.isCheckAll
? this.cartInfoList.forEach(item => {
this._shopTrue(item)
})
: this.cartInfoList.forEach(item => {
this._shopFalse(item)
})
},
_totalPeice () {
// 每次調(diào)用此方法,將初始值為0,遍歷價(jià)格并累加
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: {
// 深度監(jiān)聽所有數(shù)據(jù),每次改變重新計(jì)算總價(jià)和總數(shù)
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%;
}
}
}
關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)購物車功能(商品分類)
- Vue+Node實(shí)現(xiàn)商品列表的分頁、排序、篩選,添加購物車功能詳解
- Vue實(shí)現(xiàn)商品飛入購物車效果(電商項(xiàng)目)
- vue.js購物車添加商品組件的方法
- Vue商品控件與購物車聯(lián)動(dòng)效果的實(shí)例代碼
- Vue實(shí)現(xiàn)購物車的全選、單選、顯示商品價(jià)格代碼實(shí)例
- vue+vant-UI框架實(shí)現(xiàn)購物車的復(fù)選框全選和反選功能
- 基于Vuejs實(shí)現(xiàn)購物車功能
- vue實(shí)現(xiàn)購物車小案例
- vue實(shí)現(xiàn)商品購物車全選反選
相關(guān)文章
在Vue中實(shí)現(xiàn)網(wǎng)頁截圖與截屏功能詳解
在Web開發(fā)中,有時(shí)候需要對網(wǎng)頁進(jìn)行截圖或截屏,Vue作為一個(gè)流行的JavaScript框架,提供了一些工具和庫,可以方便地實(shí)現(xiàn)網(wǎng)頁截圖和截屏功能,本文將介紹如何在Vue中進(jìn)行網(wǎng)頁截圖和截屏,需要的朋友可以參考下2023-06-06
Vue-Router實(shí)現(xiàn)組件間跳轉(zhuǎn)的三種方法
這篇文章主要為大家詳細(xì)介紹了Vue-Router來實(shí)現(xiàn)組件間跳轉(zhuǎn)的三種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
vue中導(dǎo)出Excel表格的實(shí)現(xiàn)代碼
項(xiàng)目中我們可能會(huì)碰到導(dǎo)出Excel文件的需求,這篇文章主要介紹了vue中導(dǎo)出Excel表格的實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
前端Vue3引入高德地圖并展示行駛軌跡動(dòng)畫的步驟
最近在Vue項(xiàng)目中引入高德地圖,實(shí)現(xiàn)地圖展示與交互的方法和技術(shù),這里跟大家分享下,這篇文章主要給大家介紹了關(guān)于前端Vue3引入高德地圖并展示行駛軌跡動(dòng)畫的相關(guān)資料,需要的朋友可以參考下2024-09-09
Element-Plus實(shí)現(xiàn)動(dòng)態(tài)渲染圖標(biāo)的示例代碼
在Element-Plus中,我們可以使用component標(biāo)簽來動(dòng)態(tài)渲染組件,本文主要介紹了Element-Plus?實(shí)現(xiàn)動(dòng)態(tài)渲染圖標(biāo)教程,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
vue 解決mintui彈窗彈起來,底部頁面滾動(dòng)bug問題
這篇文章主要介紹了vue 解決mintui彈窗彈起來,底部頁面滾動(dòng)bug問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

