<div id="app" v-cloak>
<div class="header">购物车div>
<table cellpadding="5" cellspacing="0">
<tr>
<td><input type="checkbox"/>td>
<td><img src="images/p1.jpg"/>td>
<td>五香瓜子<span>19span>td>
<td>
<button>-button>
<input type="text" value="1"/>
<button>+button>
td>
tr>
<tr>
<td><input type="checkbox"/>td>
<td><img src="images/p1.jpg"/>td>
<td>五香瓜子<span>19span>td>
<td>
<button>-button>
<input type="text" value="1"/>
<button>+button>
td>
tr>
<tr>
<td><input type="checkbox"/>td>
<td><img src="images/p1.jpg"/>td>
<td>五香瓜子<span>19span>td>
<td>
<button>-button>
<input type="text" value="1"/>
<button>+button>
<button>删除button>
td>
tr>
table>
<div class="footer">
<input type="checkbox"/>全选
<i>总数量:100i>
<i>总价:1000i>
div>
div>
没有CSS文件,将就着看吧,重要的是实现逻辑

data: {
msg: "Hello world",
active: 0,
//模拟购物车数据
cartlist: [
{
id: 1,
imgurl: 'https://cdn.pixabay.com/photo/2016/01/05/13/58/apple-1122537__480.jpg',
title: '苹果',
price: 30,
num: 1,
check: true //记录是否选中
},
{
id: 2,
imgurl: 'https://cdn.pixabay.com/photo/2016/11/22/21/59/peanuts-1850809__480.jpg',
title: '花生',
price: 30,
num: 1,
check: false //记录是否选中
},
{
id: 3,
imgurl: 'https://cdn.pixabay.com/photo/2017/01/10/19/05/watermelon-1969949__480.jpg',
title: '西瓜',
price: 30,
num: 2,
check: false //记录是否选中
},
]
},
<table cellpadding="5" cellspacing="0">
<tr v-for="(item,i) in cartlist" :key="i">
<td v-if="item.check"><input type="checkbox" checked/>td>
<td><img :src="item.imgurl"/>td>
<td>{{item.title}}<span>{{item.price}}span>td>
<td>
<button>-button>
<input type="text" :value="item.num"/>
<button>+button>
td>
tr>
table>

methods: {
checkbtn(i){
console.log(this.cartlist[i])
this.cartlist[i].check = !this.cartlist[i].check
}
}

methods: {
checkbtn(i){
console.log(this.cartlist[i])
this.cartlist[i].check = !this.cartlist[i].check
this.getAllNum()
this.getAllPrice()
},
getAllNum(){
var num = 0
for(var i = 0 ; i < this.cartlist.length ;i++){
if (this.cartlist[i].check == true){
num += parseInt(this.cartlist[i].num)
}
}
this.allNum = num
},
getAllPrice(){
var num = 0
for(var i = 0 ; i < this.cartlist.length ; i++){
if (this.cartlist[i].check == true){
num += parseInt(this.cartlist[i].num) * this.cartlist[i].price
}
}
this.allPrice = num
}


getAllCheck(){
var num = 0
for(var i = 0 ; i < this.cartlist.length ; i ++){
if (this.cartlist[i].check == true){
num ++
}
}
this.allCheck = num
}

状态2
allCheckbtn(e){
//判断全选复选框是否选中
console.log(e.target.checked)
if (e.target.checked){
for(var i = 0 ; i < this.cartlist.length ; i ++){
this.cartlist[i].check = true
}
this.getAllNum()
this.getAllPrice()
}
else{
for(var i = 0 ; i < this.cartlist.length ; i ++){
this.cartlist[i].check = false
}
this.getAllNum()
this.getAllPrice()
}
}


【完整代码】
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>简单版购物车实例title>
<link rel="stylesheet" href="css/style.css">
<style>
img {
width: 200px;
height: 150px;
}
style>
head>
<body>
<div id="app" v-cloak>
<div class="header">购物车div>
<table cellpadding="5" cellspacing="0">
<tr v-for="(item,i) in cartlist" :key="i">
<td v-if="item.check"><input type="checkbox" checked @click="checkbtn(i)"/>td>
<td v-else><input type="checkbox" @click="checkbtn(i)"/>td>
<td><img :src="item.imgurl"/>td>
<td>{{item.title}}<span>{{item.price}}span>td>
<td>
<button @click="min(i)">-button>
<input type="text" :value="item.num"/>
<button @click="add(i)">+button>
td>
tr>
table>
<div class="footer">
<em v-if="allCheck==cartlist.length">
<input type="checkbox" checked @click="allCheckbtn"/>全选
em>
<em v-else>
<input type="checkbox" @click="allCheckbtn"/>全选
em>
<i>总数量:{{allNum}}i>
<i>总价:{{allPrice}}i>
div>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var vm = new Vue({
el: '#app',
data: {
msg: "Hello world",
active: 0,
//模拟购物车数据
allCheck : 0, //选中总条数
allPrice : 0,
allNum : 0,
cartlist: [
{
id: 1,
imgurl: 'https://cdn.pixabay.com/photo/2016/01/05/13/58/apple-1122537__480.jpg',
title: '苹果',
price: 30,
num: 1,
check: true //记录是否选中
},
{
id: 2,
imgurl: 'https://cdn.pixabay.com/photo/2016/11/22/21/59/peanuts-1850809__480.jpg',
title: '花生',
price: 10,
num: 1,
check: false //记录是否选中
},
{
id: 3,
imgurl: 'https://cdn.pixabay.com/photo/2017/01/10/19/05/watermelon-1969949__480.jpg',
title: '西瓜',
price: 50,
num: 2,
check: false //记录是否选中
},
]
},
methods: {
checkbtn(i){
console.log(this.cartlist[i])
this.cartlist[i].check = !this.cartlist[i].check
this.getAllNum()
this.getAllPrice()
this.getAllCheck()
},
getAllNum(){
var num = 0
for(var i = 0 ; i < this.cartlist.length ;i++){
if (this.cartlist[i].check == true){
num += parseInt(this.cartlist[i].num)
}
}
this.allNum = num
},
getAllPrice(){
var num = 0
for(var i = 0 ; i < this.cartlist.length ; i++){
if (this.cartlist[i].check == true){
num += parseInt(this.cartlist[i].num) * this.cartlist[i].price
}
}
this.allPrice = num
},
getAllCheck(){
var num = 0
for(var i = 0 ; i < this.cartlist.length ; i ++){
if (this.cartlist[i].check == true){
num ++
}
}
this.allCheck = num
},
allCheckbtn(e){
//判断全选复选框是否选中
console.log(e.target.checked)
if (e.target.checked){
for(var i = 0 ; i < this.cartlist.length ; i ++){
this.cartlist[i].check = true
}
this.getAllNum()
this.getAllPrice()
}
else{
for(var i = 0 ; i < this.cartlist.length ; i ++){
this.cartlist[i].check = false
}
this.getAllNum()
this.getAllPrice()
}
},
add(i){
this.cartlist[i].num ++
this.getAllNum()
this.getAllPrice()
},
min(i){
if (this.cartlist[i].num == 1){
return
}
this.cartlist[i].num --
this.getAllNum()
this.getAllPrice()
}
}
})
script>
body>
html>
运行效果

但是哈,我发现这样是有个bug的,

就是选中状态没变,判断的时候是正确的,视图层的问题…