1、基本环境搭建
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<script src="./js/vue.js"></script>
<body>
<div id="app">
<table>
<thead>
<tr>
<th></th>
<th>书籍</th>
<th>出版日期</th>
<th>价格</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in books">
<td>{
{item.id}}</td>
<td>{
{item.name}}</td>
<td>{
{item.date}}</td>
<td>{
{item.price}}</td>
<td>
<button>
-
</button>
1
<button>
+
</button>
</td>
<td>
<button>移除</button>
</td>
</tr>
</tbody>
</table>
</div>
<script src="main.js"></script>
</body>
</html>
main.js
const app = new Vue({
el: "#app",
data:{
books: [
{
id: 1,
name: '《大话数据结构》',
date: '2020-09-13',
price: 51.00,
count: 2
},
{
id: 2,
name: '《Java从入门到精通》',
date: '2020-09-13',
price: 69.00,
count: 1
},
{
id: 3,
name: '《Python爬虫全套》',
date: '2020-09-13',
price: 99.00,
count: 3
},
{
id: 4,
name: '《Hadoop入门到精通》',
date: '2020-09-13',
price: 88.00,
count: 5
}
]
}
})
style.css
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
}
th,td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th {
background-color: #f7f7f7;
color: #5c6b77;
font-weight: 600;
}
搭建结果
简单讲述搭建过程
2、使用过滤器对价格进行优化
实现结果
3、改变购买数量
通过v-bind,当数量小于等于1时,使减号按钮不能点击
4、实现移除按钮并计算总价格
全部移除后出现以下结果,这当然是不行的
优化后
计算总价格
5、完整代码
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<script src="./js/vue.js"></script>
<body>
<div id="app">
<div v-if="books.length">
<table>
<thead>
<tr>
<th></th>
<th>书籍</th>
<th>出版日期</th>
<th>价格</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in books">
<td>{
{item.id}}</td>
<td>{
{item.name}}</td>
<td>{
{item.date}}</td>
<td>{
{item.price | showPrice}}</td>
<td>
<button @click="decrement(index)" v-bind:disabled="item.count <= 1">
-
</button>
{
{item.count}}
<button @click="increment(index)">
+
</button>
</td>
<td>
<button @click="remove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<h2>总价格:{
{totalPrice | showPrice}}</h2>
</div>
<h2 v-else>购物车为空</h2>
</div>
<script src="main.js"></script>
</body>
</html>
main.js
const app = new Vue({
el: "#app",
data:{
books: [
{
id: 1,
name: '《大话数据结构》',
date: '2020-09-13',
price: 51.00,
count: 2
},
{
id: 2,
name: '《Java从入门到精通》',
date: '2020-09-13',
price: 69.00,
count: 1
},
{
id: 3,
name: '《Python爬虫全套》',
date: '2020-09-13',
price: 99.00,
count: 3
},
{
id: 4,
name: '《Hadoop入门到精通》',
date: '2020-09-13',
price: 88.00,
count: 5
}
]
},
filters: {
showPrice(price){
return '¥' + price.toFixed(2);
}
},
methods:{
increment(index){
this.books[index].count++
},
decrement(index){
this.books[index].count--
},
remove(index){
this.books.splice(index,1)
}
},
computed:{
totalPrice(){
let allPrice = 0;
for(let i = 0;i < this.books.length;i++){
allPrice += this.books[i].price * this.books[i].count
}
return allPrice
}
}
})
style.css
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
}
th,td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th {
background-color: #f7f7f7;
color: #5c6b77;
font-weight: 600;
}
博主会持续更新,有兴趣的小伙伴可以点赞、关注和收藏下哦,你们的支持就是我创作最大的动力!
博主开源Python爬虫教程目录索引(宝藏教程,你值得拥有!)