猿问

有谁能在这个基础上完善这个功能啊?我是十分感谢了

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
<title>vue购物车</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
    </head>
    <body>
        <div id="app" v-cloak>
<template v-if="list.length">
<table class="table table-hover talbe-striped">
<tr><input type="checkbox" @click="checkedAll" v-model="allCheck"></tr>
<tr>{{allCheck}}{{checkModel}}</tr>
<tr>商品名称</tr>
<tr>单价</tr>
<tr>数量</tr>
<tr>操作</tr>
<tr v-for="(item,index) in list">
<td><input type="checkbox" v-model="list[index].isChecked" @click="isAllCheck"></td>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>{{item.isChecked}}</td>
<td>
<button @click="reduceNum(index)">-</button>
{{item.count}}
<button @click="addNum(index)">+</button>
</td>
<td>
<button @click="removeItem(index)">移除</button>
</td>
</tr>
</table>
<div>总价:¥{{totalPrice}}</div>
</template>
<div v-else>购物车为空,现在去选购吧!</div>
        </div>
    </body>
    

<script type="text/javascript" src="https://cdn.bootcss.com/vue/2.5.3/vue.min.js"></script>
<script src="index.js"></script>
</html>
var app = new Vue({
el:"#app",
data:{
allCheck:false,
list:[
{id:1,name:'小米6',price:2499,count:2,isChecked:false},
{id:2,name:'充电宝',price:25,count:6,isChecked:false},
{id:3,name:'荣耀10',price:2699,count:4,isChecked:false},
{id:4,name:'苹果X',price:8499,count:3,isChecked:false},
],
checkModel:0
},
computed:{
totalPrice:function(){
var total = 0;
for(var i = 0;i < this.list.length;i++){
var item = this.list[i];
if(item.isChecked){
total +=item.price * item.count;}
}
return total.toString().replace(/\B(?=(\d{3})+$)/g,',');
}
},
methods:{
reduceNum:function(index){
if(this.list[index].count === 1) return;
this.list[index].count--;
},
addNum:function(index){
this.list[index].count++;
},
removeItem:function(index){
this.list.splice(index,1);
},

checkedAll:function(){
for(var i = 0;i < this.list.length;i++){
this.list[i].isChecked = !this.allCheck;
}
},
isAllCheck:function(){
}

}
})

需要添加一个全选功能,只有勾选的商品计入总价,所有商品勾选上,全选也勾选

西兰花伟大炮
浏览 2015回答 2
2回答

堂堂堂堂糖糖糖童鞋

isAllCheck: function () {   this.$nextTick(_ => {     this.allCheck = this.list.length === this.list.filter(item => {       return item.isChecked     }).length   }) }

堂堂堂堂糖糖糖童鞋

Vue 做这个功能应该挺简单的
随时随地看视频慕课网APP
我要回答