Vue实现简单的购物车、表单添加
HTML代码
<!DOCTYPE html>
<html lang="en">
<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>
<link rel="stylesheet" href="./css/bootstrap.css">
<script src="./js/vue.js"></script>
<style>
* {
font-size: 18px;
}
tr,td,th{
height: 50px;
line-height: 50px;
}
</style>
</head>
<body>
<div class="container" id="app">
<h1 class="text-center">购物车</h1>
<hr>
<div class="content">
<div class="row">
<table class="table table-striped table-condensed table-hover table-responsive text-center">
<tr class="text-center" style="border-bottom: 1px solid #ccc">
<th class="text-center">序号</th>
<th class="text-center">书名</th>
<th class="text-center">作者</th>
<th class="text-center">数量</th>
<th class="text-center">单价</th>
<th class="text-center">小计</th>
<th class="text-center">操作</th>
</tr>
<template v-for="(item,index) of books">
<tr class="warning" style="border-bottom: 1px solid #ccc">
<td> {{item.ind}}</td>
<td>{{item.name}}</td>
<td>{{item.wirter}}</td>
<td><button class="btn btn-primary btn-sm"
@click="cutnum(index)"> - </button>
{{item.num}}
<button class="btn btn-primary btn-sm"
@click=" addnum(index)">+</button>
</td>
<td>{{item.pice}}</td>
<td>{{item.pice*item.num}}</td>
<td> <button @click="removebooks(index)"
:class="{'btn-info':index%2==0,'btn-primary':index%2!=0}" >删除
</button>
</td>
</tr>
</template>
</table>
<div class="pull-right">
总价:¥{{Sum}}
</div>
</div>
</div>
<h2 class="text-center">添加书籍</h2>
书名:<br>
<input type="text" class="form-control" placeholder="请输入书名" v-model="name"><br>
作者:<br>
<input type="text" class="form-control" placeholder="请输入作者" v-model="wirter"><br>
数量:<br>
<input type="text" class="form-control" placeholder="请输入数量" v-model="num"><br>
价格:<br>
<input type="text" class="form-control" placeholder="请输入价格" v-model="pice">
<br>
<button class="form-control btn-primary" @click="addbook">添加</button>
<br><br>
</div>
<script src="./js/car.js"></script>
</body>
</html>
Js代码
new Vue({
el: '#app',
data: {
ind: 2,
name: '',
wirter: '',
num: '',
pice: '',
books: [
{ ind: 1, name: '亲热天国', wirter: '自来也', num: 1, pice: 9 },
{ ind: 2, name: '汉语词典', wirter: '新华社', num: 1, pice: 99 }
],
},
methods: {
addbook() {
if (this.name !== '' && this.age !== '') {
if (Number.isNaN(Number(this.num))) {
alert('书籍数量与单价必须为数字');
} else {
this.books.push({
ind: this.ind + 1,
name: this.name,
wirter: this.wirter,
num: this.num,
pice: this.pice,
})
}
} else {
alert("书籍信息每一项均不能为空!")
}
},
addnum(index){
this.books[index].num += 1;
},
cutnum(index){
if(this.books[index].num>0){
this.books[index].num -= 1;
}
},
removebooks(index){
this.books.splice(index,index+1);
}
},
computed: {
Sum: function () {
let arr1=[0];
for(let i =0; i<this.books.length; i++){
arr1.push(this.books[i].pice*this.books[i].num);
}
function getSum(total, num) {
return total + num;
}
return arr1.reduce(getSum);
}
}
});
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f619f88cad44cb67b5a4a3e96756872f.gif)