程序通过v-for写入数据,总价自动计算,可以对书本的数量进行增加和改变,删除书本数据。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>买书表格</title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
div {
text-align: center;
}
table {
width: 500px;
margin: 10px auto;
}
td{
text-align: center;
height: 50px;
}
</style>
</head>
<body>
<div id="app">
<table border="1px" cellspacing="0" cellpadding="">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>出版日期</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in items">
<td>{{index + 1}}</td>
<td>{{item.name}}</td>
<td>{{item.time}}</td>
<td>{{item.price}}</td>
<td><button @click="jia(index)">+</button>{{item.num}}<button @click="jian(index)">-</button></td>
<td>{{item.price*item.num}}</td>
<td><button @click="del(index)">删除</button></td>
</tr>
</tbody>
</table>
总计:{{total}}
</div>
</body>
<script type="text/javascript">
const app = new Vue({
el:'#app',
data:{
items:[
{
name: 'C',
time:'2010-10-01',
price:50,
num:60,
total:'',
},
{
name: 'C++',
time:'2010-10-01',
price:50,
num:60,
},
{
name: 'C',
time:'2010-10-01',
price:50,
num:60,
}]
},
methods:{
jia:function(index){
this.items[index].num++;
},
jian:function(index){
this.items[index].num--;
},
del:function(index){
this.items.splice(index,1)
}
},
computed:{
total:function(){
let mytotal = 0;
for (var i = 0 in this.items){
mytotal += this.items[i].num * this.items[i].price;
}
return mytotal;
}
}
})
</script>
</html>
|