<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./js/vue.js"></script>
<style>
li {
display: flex;
justify-content: space-around;
text-align: center;
line-height: 100px;
height: 100px;
padding: 10px;
}
img {
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="box">
<div v-if='list.length === 0'>空空如也</div>
<template v-else>
<input type="checkbox" v-model="isAllChecked">全選/全不選
<ul>
<li v-for='(item,index) in list' :key='item.id'>
<div>
<input type="checkbox" v-model='checkgroup' :value="item" @change="checked()">
</div>
<img :src="item.pic" alt="">
<div>{{item.name}}</div>
<div>¥{{item.price}}</div>
<div>{{item.id}}</div>
<div>
<button @click="item.number--" :disabled="item.number===1">-</button>
{{item.number}}
<button @click="item.number++" :disabled="item.number === item.limit">+</button>
</div>
<div>
<button @click="handleDel(index,item.id)">刪除</button>
</div>
</li>
</ul>
<div style="text-align: right;padding: 0 50px;">
總價(jià){{sum()}}
</div>
</template>
</div>
<script>
var vm = new Vue({
el: '#box',
data: {
checkgroup: [],
isAllChecked: false,
list: [
{
"id": 1,
"name": "商品1",
"number": 1,
"pic": "https://c-ssl.duitang.com/uploads/blog/202011/16/20201116210409_fdb12.jpg",
"msg": "頭像",
"price": 10,
"limit": 1,//限購(gòu)
},
{
"id": 2,
"name": "商品2",
"number": 2,
"pic": "https://c-ssl.duitang.com/uploads/blog/202010/31/20201031204228_sewqy.jpg",
"msg": "頭像",
"price": 20,
"limit": 5,//限購(gòu)
},
{
"id": 3,
"name": "商品3",
"number": 3,
"pic": "https://c-ssl.duitang.com/uploads/item/202005/23/20200523141101_ungtj.jpg",
"msg": "頭像",
"price": 30,
"limit": 10,//限購(gòu)
},
]
},
methods: {
sum() {
var total = 0
this.checkgroup.forEach(item => {
total += item.price * item.number
})
return total
},
// 刪除方法
handleDel(index, deleteid) {
// 刪除checkgroup里面對(duì)應(yīng)的元素,通過(guò)id
this.checkgroup = this.checkgroup.filter(item => item.id !== deleteid)
// 刪除原數(shù)組,通過(guò)index
this.list.splice(index, 1)
//判定是否全勾選上
this.checked()
},
//全選/全不選
// handleChange(ev) {
// // console.log("change",ev.target.checked)
// console.log(this.isAllChecked)
// if (this.isAllChecked) {
// this.checkgroup = this.list
// } else {
// this.checkgroup = []
// }
// //判斷是否全選
// },
checked() {
if (this.checkgroup.length === this.list.length) {
this.isAllChecked = true
} else {
if(this.checkgroup.length === this.list.length){
this.isAllChecked = false
}
}
}
},
//監(jiān)聽(tīng)狀態(tài)的改變
watch:{
isAllChecked(data){
console.log("isAllChecked狀態(tài)改變了",data)
if (this.isAllChecked) {
this.checkgroup = this.list
} else {
this.checkgroup = []
}
}
}
})
</script>
</body>
</html>
18-watch-購(gòu)物車(chē)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 023|決策時(shí)間:用戶思考了很久,又把商品放回去了,怎么辦? 什么是“決策時(shí)間”?消費(fèi)者的注意力時(shí)長(zhǎng)越來(lái)越短,人們...
- <!DOCTYPE html> 雪碧圖 .bnt:link{ display: block; width: 43p...
- 紅墻,黃葉,老后窗…… 18樓比記憶里漂亮了好多! 小時(shí)候,18樓是個(gè)溫暖的安慰,也是個(gè)快樂(lè)的庇護(hù)所。 因?yàn)?,姥?..
- 如何從無(wú)到有來(lái)開(kāi)發(fā)一個(gè)項(xiàng)目 一、需求分析 額度 15000 或 自定義 ---> 注冊(cè)功能實(shí)現(xiàn)購(gòu)物商城,買(mǎi)...
- 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂(lè)有人憂愁,有人驚喜有人失落,有的覺(jué)得收獲滿滿有...