<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style>
li{
display: flex;
justify-content: space-around;
padding: 10px;
}
li img{
width: 200px;
}
</style>
<script type="text/javascript" src="vue.global.js"></script>
<body>
<div id="box">
<input type="checkbox" v-model="isAll" @change="handleAllChecked">全选/全不选
<ul>
<li v-for="(item,index) in datalist" :key="item.id">
<input type="checkbox" v-model="checkList" :value="item"
@change="handleItemChecked">
<img :src="item.pic">
<div>
<div>{{item.name}}</div>
<div style="color:red">¥{{item.price}}</div>
</div>
<div>
<button @click="item.number--" :disabled="item.number===0">-</button>
<span>{{item.number}}</span>
<button @click="item.number++" :disabled="item.number===item.limit">+</button>
</div>
<div>
<button @click="handleDeleteClick(index,item.id)">删除</button>
</div>
</li>
</ul>
<div>总金额:{{computedSum}}</div>
{{checkList}}
</div>
<script type="text/javascript">
var obj={
computed:{
computedSum(){//累加计算 checkList 数组的每一项的 价格*数量
var total=0
this.checkList.forEach(item=>{
total+=item.price*item.number
})
return total
}
},
data(){
return{
isAll:false,
checkList:[],//勾选购物车数据
datalist:[{
name:"商品1",
price:10,
number:1,
id:1,
limit:5,
pic:"http://img.duoziwang.com/2018/18/06081141563306.jpg"
},
{
name:"商品2",
price:20,
number:2,
id:2,
limit:10,
pic:"http://img.duoziwang.com/2018/18/06081141563306.jpg"
},{
name:"商品3",
price:30,
number:2,
id:3,
limit:10,
pic:src="http://img.duoziwang.com/2018/18/06081141563306.jpg"
}
]
}
},
methods:{
handleDeleteClick(index,id){
// console.log(index)
this.datalist.splice(index)
this.checkList= this.checkList.filter(item=>item.id!==id)
//同步一下状态
this.handleItemChecked()
},//全选
handleAllChecked(){
if(this.isAll){
this.checkList=this.datalist
}else{
this.checkList=[]
}
},
handleItemChecked(){
if(this.checkList.length===this.datalist.length){
this.isAll=true
}else{
this.isAll=false
}
}
}
}
Vue.createApp(obj).mount("#box")
</script>
</body>
</html>