案例-品牌管理(总价和均价)
目标: 基于之前的案例, 完成总价和均价的计算效果
此处只修改了变化的代码
<tr style="background-color: #EEE">
<td>统计:</td>
<td colspan="2">总价钱为: {{ allPrice }}</td>
<td colspan="2">平均价: {{ avgPrice }}</td>
</tr>
<script>
// 目标: 总价和均价显示
// 1. 末尾补tr - 显示总价和均价
export default {
// ...源代码省略
// 2. 计算属性
computed: {
allPrice(){
// 3. 求总价
return this.list.reduce((sum, obj) => sum += obj.price, 0)
},
avgPrice(){
// 4. 求均价 - 保留2位小数
return (this.allPrice / this.list.length).toFixed(2)
}
}
}
</script>
全部删除后会有个小 bug
解决方法:全部删除后不展示总价与均价栏( v-if )
<!-- 新增价格栏 -->
<!--如果list长度不等于0时,展示价格栏,也就是全部删除后,list长度为0时,不展示价格栏-->
<tr v-if="list.length !== 0" style="background-color:#EEE">
<td>统计:</td>
<td colspan="2">总价钱为:{{allPrice}}</td>
<td colspan="2">平均价:{{avgPrice}}</td>
</tr>
</tbody>
我的:
<template>
<div id="app">
<div class="container">
<!-- 顶部框模块 -->
<div class="form-group">
<div class="input-group">
<h4>品牌管理</h4>
</div>
</div>
<!-- 数据表格 -->
<table class="table table-bordered table-hover mt-2">
<thead>
<tr >
<th>编号</th>
<th>资产名称</th>
<th>价格</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list " :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<!-- 如果价格超过100,就有red这个类 -->
<td :class="{red:item.price>100}">{{item.price}}</td>
<td>{{item.time}}</td>
<td><a href="#" @click.prevent="removeBtn(item.id)">删除</a></td>
</tr>
<!-- 新增价格栏 -->
<tr v-if="list.length !== 0" style="background-color:#EEE">
<td>统计:</td>
<td colspan="2">总价钱为:{{allPrice}}</td>
<td colspan="2">平均价:{{avgPrice}}</td>
</tr>
</tbody>
<!-- 如果数组长度为0,就创建 tfoot 结构到页面中 -->
<tfoot v-if="list.length === 0">
<tr>
<td colspan="5" style="text-align: center">暂无数据</td>
</tr>
</tfoot>
</table>
<!-- 添加资产 -->
<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="资产名称"
v-model.trim="name"
/>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input
type="text"
class="form-control"
placeholder="价格"
v-model.number="price"
/>
</div>
</div>
<!-- 阻止表单提交 -->
<button @click.prevent="addbtn" class="btn btn-primary">添加资产</button>
</form>
</div>
</div>
</template>
<script>
import 'bootstrap/dist/css/bootstrap.css'
import moment from "moment"
export default {
data() {
return {
name: "", // 名称
price: 0, // 价格
// 有本地存储读取本地存储,没有初始化数组
list: JSON.parse(localStorage.getItem('plist')) || [
{ id: 100, name: "外套", price: 199, time: new Date('2010-08-12')},
{ id: 101, name: "裤子", price: 34, time: new Date('2013-09-01') },
{ id: 102, name: "鞋", price: 25.4, time: new Date('2018-11-22') },
{ id: 103, name: "头发", price: 19900, time: new Date('2020-12-12') }
],
};
},
// 计算属性
computed: {
// 总价:
allPrice(){
// let sum = 0
// this.list.forEach(item => {
// sum +=item.price
// })
// return sum
// reduce 数组方法
return this.list.reduce((sum,item) =>sum+=item.price,0)//sum的初始值为 0
},
// 平均价
avgPrice(){
// allPrice:依赖项
return (this.allPrice / this.list.length).toFixed(2) //.toFixed(2):保留两位小数
}
},
methods: {
// 添加按钮
addbtn(){
// console.log(1111,this.name,this.price);
if(this.name === ''){
return alert('请输入合法名称')
}
// 💥修复 id 的bug
const id = this.list.length ? this.list[this.list.length - 1].id + 1 : 200
// push 影响原来的数组,页面的会自动更新
this.list.push( {
id: id ,
name: this.name,
price:this.price,
time: new Date()
})
// 添加成功后清空两个输入框
this.name = ""
this.price = 0
},
// 删除按钮
removeBtn(id){
console.log(id);
// 过滤掉 不要某一项 但是 filter 不改变原数组
this.list = this.list.filter(v => v.id != id)
}
},
// 定义过滤器结构
filters: {
shijian:(val) => {
return moment(val).format('YYYY-MM-DD')
}
},
// watch 侦听器结构
watch:{
list (){
console.log('list发生了变化')
localStorage.setItem('plist',JSON.stringify(this.list))
}
}
};
</script>
<style >
.red{
color: red;
}
</style>
总结: 总价来源于所有数据计算而来的结果, 故采用计算属性