vue+vant實(shí)現(xiàn)購物車全選和反選功能
本文實(shí)例為大家分享了vue+vant實(shí)現(xiàn)購物車全選和反選的具體代碼,供大家參考,具體內(nèi)容如下
這是效果圖:
話不多少,直接上代碼:
<template> <div class='cart'> <div 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 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 :price='10060' button-text='去結(jié)算' @submit='onSubmit'> <van-checkbox v-model='isCheckAll' @click='checkAll()'>全選</van-checkbox> <span 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, // 價格 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, // 所有價格 allShops: 0, // 被選中的商店數(shù)量 allCount: 0 // 被選中的產(chǎn)品數(shù)量 } }, methods: { // 選中單個商品 ischeck (item, pro) { // 為未選中的時候改變?yōu)閒alse,反之為true !pro.isChecked ? this._checkTrue(item, pro) : this._checkFalse(item, pro) }, // 修改單個商品的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),每全選一個商店,被選中商店數(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 : ’’ }, // 修改單個商品的 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)為選中時改變false --this.allShops // 商店數(shù)減一 } this.isCheckAll = false // 全選狀態(tài)為false }, // 選擇整個商店的商品 checkShop (item) { !item.checked ? this._shopTrue(item) : this._shopFalse(item) }, // 遍歷商店每個商品,狀態(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)計算總價和總數(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,遍歷價格并累加 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ù),每次改變重新計算總價和總數(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決Python 進(jìn)程池Pool中一些坑2. 三個不常見的 HTML5 實(shí)用新特性簡介3. Python獲取抖音關(guān)注列表封號賬號的實(shí)現(xiàn)代碼4. Python使用jupyter notebook查看ipynb文件過程解析5. ajax請求添加自定義header參數(shù)代碼6. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法7. Python如何讀寫CSV文件8. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究9. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼10. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁
