{
"usingComponents": {
"cart-item": "/component/cart/index"
}
}
父組件index.js
Page({
data: {
cartList: [
{
shopname: '貓咪小店',
logo: '/images/avatar.jpeg',
shopid: 11,
commodity: [
{
id: 1,
image:'/images/commodity.jpg',
title: '雅詩蘭黛鮮活煥亮紅石榴晚霜50ml 補(bǔ)水保濕 滋潤排濁',
standard: '111 + 黑色',
price: '100',
stock: 10,
quantity: 1,
isselected: 0,
},
{
id: 2,
image:'/images/avatar7.jpg',
title: '雅詩蘭黛鮮活煥亮紅石榴晚霜50ml 補(bǔ)水保濕 滋潤排濁',
price: '10',
stock: 5,
quantity: 1,
isselected: 0,
}
]
},
{
shopname: '貓咪小店',
logo: '/images/avatar5.jpg',
shopid: 450,
commodity: [
{
id: 3,
image:'/images/commodity.jpg',
title: '雅詩蘭黛鮮活煥亮紅石榴晚霜50ml 補(bǔ)水保濕 滋潤排濁',
price: '90',
stock: 10,
quantity: 1,
isselected: 0,
},
{
id: 4,
image:'/images/avatar7.jpg',
title: '雅詩蘭黛鮮活煥亮紅石榴晚霜50ml 補(bǔ)水保濕 滋潤排濁',
price: '100',
stock: 5,
quantity: 1,
isselected: 0,
},
{
id: 5,
image:'/images/commodity.jpg',
title: '雅詩蘭黛鮮活煥亮紅石榴晚霜50ml 補(bǔ)水保濕 滋潤排濁',
standard: '111 + 黑色',
price: '100',
stock: 2,
quantity: 1,
isselected: 0,
}
]
},
{
shopname: '貓咪小店',
logo: '/images/avatar.jpeg',
shopid: 550,
commodity: [
{
id: 6,
image:'/images/avatar8.jpg',
title: '雅詩蘭黛鮮活煥亮紅石榴晚霜50ml 補(bǔ)水保濕 滋潤排濁',
standard: '111 + 黑色',
price: '100',
stock: 1,
quantity: 1,
isselected: 0,
}
]
},
],
/* 商品是否全選中 */
isSelectedAll: false,
/* 已選中商品的價格 */
countPrice: 0,
/* 統(tǒng)計所有選中的商品數(shù)量 */
countSelectedNum: 0,
},
/* 處理商品選中 */
handleSelect(e) {
let countPrice = 0;
let countSelectedNum = 0;
let cartList = this.data.cartList;
let length = cartList.length;
/* 因為是假數(shù)據(jù),所以需要循環(huán)查找到對應(yīng)的數(shù)據(jù)將其替換 */
for(let i = 0; i < length; i++) {
for(let j = 0; j < cartList[i].commodity.length; j++) {
if (cartList[i].commodity[j].id == e.detail.commodity.id) {
cartList[i].commodity[j] = e.detail.commodity;
cartList[i].commodity[j].selectedNum = e.detail.selectedNum;
}
if (cartList[i].commodity[j].isselected == 1) {
/* 點擊選中的時候,計算價格,要判斷下設(shè)置的商品選中數(shù)量,
* 我這里的是對點擊了的商品才設(shè)置了選中的數(shù)量,所以需要對沒有點擊的商品數(shù)量設(shè)置為1,然后就默認(rèn)的加一
*/
if (cartList[i].commodity[j].selectedNum != undefined) {
countPrice += cartList[i].commodity[j].price * cartList[i].commodity[j].selectedNum;
countSelectedNum += cartList[i].commodity[j].selectedNum
} else {
countPrice += cartList[i].commodity[j].price * 1;
countSelectedNum += 1;
}
}
}
}
/* 對是否全選中進(jìn)行判斷 */
let isSelectedAll = true;
for (let i = 0; i < length; i++) {
for (let j = 0; j < cartList[i].commodity.length; j++) {
/* 若商品中的isselecetd有為0的就終止循環(huán),直接設(shè)置為未全選 */
if (cartList[i].commodity[j].isselected == 0) {
isSelectedAll = false;
break;
}
}
}
this.setData({
cartList,
isSelectedAll,
countPrice,
countSelectedNum
})
},
/* 全選中商品 */
handleSelectAll() {
let isSelectedAll = !this.data.isSelectedAll;
let cartList = this.data.cartList;
let length = cartList.length;
let countPrice = 0;
let countSelectedNum = 0;
/* 遍歷數(shù)據(jù)中的isselected來進(jìn)行全選的操作 */
for(let i = 0; i < length; i++) {
for (let j = 0; j < cartList[i].commodity.length; j++) {
if(isSelectedAll) {
cartList[i].commodity[j].isselected = 1;
/* 全選的時候,計算價格,要判斷下設(shè)置的商品選中數(shù)量,
* 我這里的是對點擊了的商品才設(shè)置了選中的數(shù)量,所以需要對沒有點擊的商品數(shù)量設(shè)置為1,然后就默認(rèn)加一
*/
if (cartList[i].commodity[j].selectedNum != undefined) {
countPrice += parseInt(cartList[i].commodity[j].price) * cartList[i].commodity[j].selectedNum;
countSelectedNum += cartList[i].commodity[j].selectedNum;
} else {
countPrice += cartList[i].commodity[j].price * 1;
countSelectedNum += 1;
}
} else {
cartList[i].commodity[j].isselected = 0;
}
}
}
this.setData({
isSelectedAll,
cartList,
countPrice,
countSelectedNum
})
},
})
|