1. 程式人生 > 程式設計 >vue+vant實現購物車全選和反選功能

vue+vant實現購物車全選和反選功能

本文例項為大家分享了vue+vant實現購物車全選和反選的具體程式碼,供大家參考,具體內容如下

這是效果圖:

vue+vant實現購物車全選和反選功能

話不多少,直接上程式碼:

<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,// 選擇全部商店的商品,已經計算總價和總數量的函式
 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.js元件的教程,請大家點選專題vue.js元件學習教程進行學習。

更多vue學習教程請閱讀專題《vue實戰教程》

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。