<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>todolist加购物车</title>
<link rel="stylesheet" href="css/bootstrap.css" />
<script type="text/javascript" src="js/vue.js" ></script>
</head>
<body>
<div id="app">
<div class="container">
<table class="table table-bordered table-hover text-center">
<tr>
<td>
<input type="checkbox" v-model="checkAll" @click="selectAll"/>
</td>
<td>
商品名称
</td>
<td>
商品价格
</td>
<td>
商品数量
</td>
<td>
商品总额
</td>
<td>
操作
</td>
</tr>
<tr v-for="(item,index) in listInfo">
<td>
<input
type="checkbox"
:value="item.id"
v-model="checkItem"
@change="selectOne(index)"
>
</td>
<td>{
{
item.shopName}}</td>
<td>{
{
item.shopPrice}}</td>
<td>
<button class="btn " @click="reduce(index)">-</button>
<input type="text" v-model="item.shopCount" />
<button class="btn " @click="add(index)">+</button>
</td>
<td>
{
{
item.shopPrice*item.shopCount}}
</td>
<td>
<button class="btn btn-danger" @click="del(index)">删除</button>
</td>
</tr>
</table>
<p class="text-right">
金额总计:{
{
sum}}
</p>
<p class="text-right">
商品数量:{
{
count}}
</p>
<hr />
<form>
<div class="form-group">
<input class="form-control" placeholder="商品名称" v-model="shopName" />
</div>
<div class="form-group">
<input class="form-control" placeholder="商品价格" v-model = "shopPrice"/>
</div>
<div class="form-group">
<button class="btn btn-success" type="button" @click="addInfo">增加</button>
</div>
</form>
</div>
</div>
<script>
new Vue({
el:"#app",
data:{
listInfo:[
{
id:1,shopName:"帽子",shopPrice:100,shopCount:1,isBuy:false},
{
id:2,shopName:"衣服",shopPrice:100,shopCount:1,isBuy:false},
{
id:3,shopName:"裤子",shopPrice:100,shopCount:1,isBuy:false},
{
id:4,shopName:"袜子",shopPrice:100,shopCount:1,isBuy:false},
{
id:5,shopName:"鞋子",shopPrice:100,shopCount:1,isBuy:false},
],
shopName:"",
shopPrice:"",
checkItem:[],
checkAll:false,
checkNum:0,
totalPrice:0,
aaArr:[],
},
methods:{
add:function(index){
this.listInfo[index].shopCount++
},
reduce:function(index){
if(this.listInfo[index].shopCount<=0){
this.listInfo[index].shopCount = 0
}else {
this.listInfo[index].shopCount--
}
},
addInfo:function(){
var obj = {
id:this.listInfo.length+1,
shopName:this.shopName,
shopPrice:this.shopPrice,
shopCount:0,
isBuy:false
}
console.log(obj)
this.listInfo.push(obj)
},
del(index){
if(confirm("确定完成了吗?")){
this.listInfo.splice(index,1)
}
},
selectOne(index,item){
if(this.checkItem.length == this.listInfo.length){
this.checkAll = true
}else {
this.checkAll = false
}
if(this.listInfo[index].isBuy){
this.listInfo[index].isBuy = false
}else{
this.listInfo[index].isBuy = true
}
let isArr = []
this.listInfo.map((item, index) => {
if(item.isBuy){
isArr.push(item)
}
})
this.aaArr = []
this.totalPrice = 0
isArr.map(( item, index) => {
let aa = item.id+'_'+ item.shopPrice
this.aaArr.push(aa)
this.totalPrice +=item.shopPrice
})
},
selectAll(){
this.checkItem = []
this.aaArr = []
this.totalPrice = 0
if(!this.checkAll){
for (var i=0;i<this.listInfo.length;i++) {
let aa = this.listInfo[i].id+'_'+ this.listInfo[i].shopPrice
this.totalPrice +=this.listInfo[i].shopPrice
this.checkItem.push(this.listInfo[i].id)
this.aaArr.push(aa)
this.listInfo[i].isBuy = true
this.checkAll = true
}
}else {
this.checkItem = []
this.checkAll = false
for (var i=0;i<this.listInfo.length;i++) {
this.listInfo[i].isBuy = false
}
}
}
},
computed:{
sum(){
var total = 0
for (var i=0;i<this.listInfo.length;i++) {
if(this.listInfo[i].isBuy){
total+=parseFloat(this.listInfo[i].shopPrice)*parseFloat(this.listInfo[i].shopCount)
}
}
return total
},
count:function(){
var total = 0
for (var i=0;i<this.listInfo.length;i++) {
if(this.listInfo[i].isBuy){
total+=parseInt(this.listInfo[i].shopCount)
}
}
return total
}
}
})
</script>
</body>
</html>