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國(guó)航筆記本', // 產(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組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(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?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題
這篇文章主要介紹了vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03ElementUI多個(gè)子組件表單的校驗(yàn)管理實(shí)現(xiàn)
這篇文章主要介紹了ElementUI多個(gè)子組件表單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載
這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Vue.js實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)總結(jié)及實(shí)現(xiàn)思路
這篇文章主要介紹了 Vue.js實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)總結(jié)及實(shí)現(xiàn)思路,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Vue-cli3項(xiàng)目引入Typescript的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue-cli3項(xiàng)目引入Typescript的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10