91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Vue實現購物車場景下的應用

發布時間:2020-09-16 22:11:04 來源:腳本之家 閱讀:179 作者:Henry2509885 欄目:web開發

本文實例為大家分享了Vue在購物車場景下的應用,供大家參考,具體內容如下

購物車場景需求:

1. 商品、店鋪、購物車的選擇

2. 商品刪除

關鍵代碼

測試數據

var _list = [{
 checked: false,
 goods: [{
  name: "商品1",
  price: 23,
  checked: false
 }]
}, {
 checked: false,
 goods: [{
  name: "商品2",
  price: 20,
  checked: false
 }, {
  name: "商品3",
  price: 30,
  checked: false
 }]
}];

店鋪對象組成購物車數組 _list
商品對象組成店鋪對象下的商品數組 goods
每個商品和店鋪都有自己的 checked 字段

一. 選擇商品

  • 選擇商品
  • 選擇店鋪下的所有商品
  • 選擇購物車里的所有商品

數據綁定

<body class="app">
 <input type="checkbox" v-if="list.length" v-model="checkedAll">
 <div v-for="(shopI, shop) in list" class="shop">
  <input type="checkbox" v-model="shop.checked">
  <div v-for="(goodI, good) in shop.goods" class="good">
   <input type="checkbox" v-model="good.checked">
   <span v-html="good.name"></span>
   <span v-html="good.age"></span>
  </div>
 </div>
</body>
var vue = new Vue({
 el: '.app',
 data: {
  list: _list
 }
});

監聽購物車全選

computed: {
 checkedAll: function() {
  return this.checkedShopsCount == this.list.length;
 },
 checkedShopsCount: function() {
  var i = 0;
  this.list.forEach(function(item) {
   if (item.checked == true) i++;
  });
  return i;
 }
}

當店鋪選擇數(checkedShopsCount)等于店鋪數(list.length)時,購物車全選(checkedAll)為 true
商品 & 店鋪選擇事件

<input type="checkbox" v-if="list.length" v-model="checkedAll" @change="setAllChecked($event)">
<div v-for="(shopI, shop) in list">
 <input type="checkbox" v-model="shop.checked" @change="setGoodChecked(shop)">
 <div v-for="(goodI, good) in shop.goods">
  <input type="checkbox" v-model="good.checked" @change="setShopChecked(good, shop)">
  <span v-html="good.name"></span>
  <span v-html="good.age"></span>
 </div>
</div>
methods: {
 setAllChecked: function(e) {
  this.list.forEach(function(shop) {
   shop.checked = e.target.checked;
   shop.goods.forEach(function(good) {
    good.checked = e.target.checked;
   });
  });
 },
 setGoodChecked: function(shop) {
  shop.goods.forEach(function(good) {
   good.checked = shop.checked;
  });
 },
 setShopChecked: function(good, shop) {
  if (!good.checked) {
   shop.checked = false;
  } else {
   // 檢查店鋪是否存在 checked 為 false 的商品
   shop.checked = !shop.goods.find(function(good) {
    return !good.checked;
   });
  }
 }
}

選擇購物車時執行 setAllChecked,此時選擇所有商品和店鋪
選擇店鋪時執行 setGoodChecked,此時選擇該店鋪下所有商品
選擇商品時執行 setShopChecked
    當商品 checked 為 false 時,取消選擇當前店鋪
    當商品 checked 為 true 時,判斷當前店鋪是否選擇了所有商品,是則選擇當前店鋪,否則取消選擇當前店鋪

二、刪除商品

<input type="button" value="刪除" @click="deleteGood(shop,goodI,list,shopI)">
methods: {
 deleteGood: function(shop,goodI,list,shopI) {
  shop.goods.splice(goodI, 1);
  if (shop.goods.length==0) {
   list.splice(shopI, 1);
  }
 }
}

全部代碼

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的購物車</title>
<script src="vue.min.js"></script>
</head>
<body class="app">
<input type="checkbox" v-if="list.length" v-model="checkedAll" @change="setAllChecked($event)">
<div v-for="(shopI, shop) in list">
<input type="checkbox" v-model="shop.checked" @change="setGoodChecked(shop)">
<div v-for="(goodI, good) in shop.goods">
<input type="checkbox" v-model="good.checked" @change="setShopChecked(good, shop)">
<span v-html="good.name"></span>
<span v-html="good.age"></span>
<input type="button" value="刪除" @click="deleteGood(shop,goodI,list,shopI)">
</div>
</div>
</body>
<script>
var _list = [{
checked: false,
goods: [{
name: "商品1",
money: 23,
checked: false
}]
}, {
checked: false,
goods: [{
name: "商品2",
money: 20,
checked: false
}, {
name: "商品3",
money: 30,
checked: false
}]
}];
var vue = new Vue({
el: '.app',
data: {
list: _list
},
computed: {
checkedAll: function() {
return this.checkedShopsCount == this.list.length;
},
checkedShopsCount: function() {
var i = 0;
this.list.forEach(function(item) {
if (item.checked == true) i++;
});
return i;
}
},
methods: {
setGoodChecked: function(shop) {
shop.goods.forEach(function(good) {
good.checked = shop.checked;
});
},
setShopChecked: function(good, shop) {
if (!good.checked) {
shop.checked = false;
} else {
shop.checked = !shop.goods.find(function(good) {
return !good.checked;
});
}
},
setAllChecked: function(e) {
this.list.forEach(function(shop) {
shop.checked = e.target.checked;
shop.goods.forEach(function(good) {
good.checked = e.target.checked;
});
});
},
deleteGood: function(shop, goodI, list, shopI) {
shop.goods.splice(goodI, 1);
if (shop.goods.length == 0) {
list.splice(shopI, 1);
}
}
}
});
</script>
</html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宁阳县| 基隆市| 沙坪坝区| 德令哈市| 体育| 天长市| 安新县| 邵阳县| 麦盖提县| 吉安县| 和平县| 乐都县| 任丘市| 河东区| 镇江市| 育儿| 乐东| 得荣县| 灵山县| 乐陵市| 天津市| 永福县| 陵水| 栾川县| 八宿县| 祁门县| 吴忠市| 榆社县| 鹤庆县| 陈巴尔虎旗| 吉木乃县| 石渠县| 闻喜县| 平邑县| 怀化市| 高碑店市| 奉节县| 双峰县| 广宗县| 沛县| 项城市|