vue+vant實現(xiàn)購物車全選和反選功能
本文實例為大家分享了vue+vant實現(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">不含運費</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: '錘子手機手感保護膜', category: '登陸月球', price: 9.9, count: 1 }, { isChecked: false, productTitle: '錘子手機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)商店狀態(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()方法中將商店狀態(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組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題
這篇文章主要介紹了vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03原生JS?Intersection?Observer?API實現(xiàn)懶加載
這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07Vue.js實現(xiàn)大轉(zhuǎn)盤抽獎總結(jié)及實現(xiàn)思路
這篇文章主要介紹了 Vue.js實現(xiàn)大轉(zhuǎn)盤抽獎總結(jié)及實現(xiàn)思路,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Vue-cli3項目引入Typescript的實現(xiàn)方法
這篇文章主要介紹了Vue-cli3項目引入Typescript的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10