![1682071972085]()
需求 & 思路分析
1.基本渲染
- 立刻發送請求獲取數據 created
- 拿到數據,存到data的響應式數據中
- 結合數據,進行渲染 v-for
- 消費統計 —> 計算屬性
2.添加功能
- 收集表單數據 v-model,使用指令修飾符處理數據
- 給添加按鈕注冊點擊事件,對輸入的內容做非空判斷,發送請求
- 請求成功后,對文本框內容進行清空
- 重新渲染列表
3.刪除功能
- 注冊點擊事件,獲取當前行的id
- 根據id發送刪除請求
- 需要重新渲染
4.餅圖渲染
- 初始化一個餅圖 echarts.init(dom) mounted鉤子中渲染
- 根據數據試試更新餅圖 echarts.setOptions({...})
![image]()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- CSS only -->
<link rel="stylesheet" />
<style>
.red {
color: red !important;
}
.search {
width: 300px;
margin: 20px 0;
}
.my-form {
display: flex;
margin: 20px 0;
}
.my-form input {
flex: 1;
margin-right: 20px;
}
.table> :not(:first-child) {
border-top: none;
}
.contain {
display: flex;
padding: 10px;
}
.list-box {
flex: 1;
padding: 0 30px;
}
.list-box a {
text-decoration: none;
}
.echarts-box {
width: 600px;
height: 400px;
padding: 30px;
margin: 0 auto;
border: 1px solid #ccc;
}
tfoot {
font-weight: bold;
}
@media screen and (max-width: 1000px) {
.contain {
flex-wrap: wrap;
}
.list-box {
width: 100%;
}
.echarts-box {
margin-top: 30px;
}
}
</style>
</head>
<body>
<div id="app">
<div class="contain">
<!-- 左側列表 -->
<div class="list-box">
<!-- 添加資產 -->
<form class="my-form">
<input v-model.trim="name" type="text" class="form-control" placeholder="消費名稱" />
<input v-model.number="price" type="text" class="form-control" placeholder="消費價格" />
<button @click="add" type="button" class="btn btn-primary">添加賬單</button>
</form>
<table class="table table-hover">
<thead>
<tr>
<th>編號</th>
<th>消費名稱</th>
<th>消費價格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in list" :key="item.id">
<td>{{ index + 1 }}</td>
<td>{{ item.name }}</td>
<td :class="{ red : item.price >= 500 }">{{ item.price.toFixed(2) }}</td>
<td><a @click="del(item.id)" href="javascript:;">刪除</a></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">消費總計: {{ totalPrice.toFixed(2) }}</td>
</tr>
</tfoot>
</table>
</div>
<!-- 右側圖表 -->
<div class="echarts-box" id="main"></div>
</div>
</div>
<script src="echarts.min.js"></script>
<script src="vue.js"></script>
<script src="axios.js"></script>
<script>
/**
* 接口文檔地址:
* http://localhost:3001
*
* 功能需求:
* 1. 基本渲染
* (1) 立刻發送請求獲取數據 created
* (2) 拿到數據,存到data的響應式數據中
* (3) 結合數據,進行渲染 v-for
* (4) 消費統計 => 計算屬性
* 2. 添加功能
* (1) 收集表單數據 v-model
* (2) 給添加按鈕注冊點擊事件,發送添加請求
* (3) 需要重新渲染
* 3. 刪除功能
* (1) 注冊點擊事件,傳參傳 id
* (2) 根據 id 發送刪除請求
* (3) 需要重新渲染
* 4. 餅圖渲染
* (1) 初始化一個餅圖 echarts.init(dom) mounted鉤子實現
* (2) 根據數據實時更新餅圖 echarts.setOption({ ... })
*/
const app = new Vue({
el: '#app',
data: {
list: [],
name: '',
price: ''
},
// 生命周期鉤子
async created() {
// const res = await axios('http://localhost:3001/bill', {
// params: {
// creator: '小黑'
// }
// })
// this.list = res.data
this.getList()
},
// 餅圖在mounted鉤子中實現
mounted() {
// 基于準備好的dom,初始化echarts實例
this.myChart = echarts.init(document.getElementById('main'))
// 繪制圖表 (官網直接引入:ctrl cv)
this.myChart.setOption({
// 大標題
title: {
text: '消費賬單列表',
subtext: 'Fake Data',
left: 'center'
},
// 提示框
tooltip: {
trigger: 'item'
},
// 圖例
legend: {
orient: 'vertical',
left: 'left'
},
// 數據項
series: [
{
name: '消費賬單',
type: 'pie',
radius: '50%', // 半徑
data: [
// { value: 1048, name: 'Search Engine' },
// { value: 735, name: 'Direct' }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
})
},
computed: {
totalPrice() {
return this.list.reduce((sum, item) => sum + item.price, 0)
}
},
methods: {
async getList() {
const res = await axios('http://localhost:3001/bill', {
params: {
creator: '小黑'
}
})
this.list = res.data
// 異步更新餅圖數據
this.myChart.setOption({
// 數據項
series: [
{
data: this.list.map(item => ({ value: item.price, name: item.name })) // 返回的是對象,要添加括號表示是對象而不是代碼塊
}
]
})
},
async add() {
if (!this.name === '' || !this.price === '' || typeof this.price !== 'number') {
alert('請輸入完整且正確的數據')
return
}
// 發送添加請求
const res = await axios.post('http://localhost:3001/bill', {
id: (+new Date()).toString, // 自行補充的部分
creator: '小黑',
price: this.price,
name: this.name
})
// 重新渲染一次
this.getList()
this.name = ''
this.price = ''
},
async del(id) {
// console.log(id)
const res = await axios.delete(`http://localhost:3001/bill/${id}`)
this.getList()
}
}
})
</script>
</body>
</html>