<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="JS/vue.js"></script>
<style>
table{
width:1200px;
margin:0 auto;
text-align: center;
}
</style>
</head>
<body>
<div id="add">
<table border="1" cellspacing="0">
<thead>
<tr>
<td>编号</td>
<td>名称</td>
<td>单价</td>
<td>数量</td>
<td>总价</td>
</tr>
</thead>
<tbody>
<tr v-for="(val,index) in arr">
<td>{{index+1}}</td>
<td>{{val.name}}</td>
<td>{{val.uprice}}</td>
<td>
<button v-on:click="jian(index)">-</button>
{{val.case}}
<button v-on:click="jia(index)">+</button>
</td>
<td>{{val.uprice*val.case}}</td>
</tr>
<tr >
<td></td>
<td></td>
<td></td>
<td></td>
<td>总计{{arr1}}</td>
</tr>
</tbody>
</table>
</div>
<script>
new Vue({
el:"#add",
data:{
arr:[
{
name:"香蕉",
uprice:1,
case:0,
price:0
},
{
name:"苹果",
uprice:2,
case:0,
price:0
},
{
name:"鸭梨",
uprice:3,
case:0,
price:0
}
],
arr1:0
},
methods:{
jian:function(i){
if(this.arr[i].case>=1){
this.arr[i].case-=1,
/* this.arr[i].price=this.arr[i].case*this.arr[i].uprice,*/
this.arr1-=this.arr[i].uprice
}
},
jia:function(i){
this.arr[i].case+=1,
/* this.arr[i].price=this.arr[i].case*this.arr[i].uprice,*/
this.arr1+=this.arr[i].uprice
}
}
})
</script>
</body>
</html>
效果: