RequestChartTable.vue
4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!-- 查询数据表格或图表展示 -->
<script>
export default {
components: {
ChartLine: defineAsyncComponent(() => import('@/components/ChartLine')),
ChartBar: defineAsyncComponent(() => import('@/components/ChartBar')),
},
props: {
// 标题
title: {},
// 数据展示图表类型
type: {
type: String, // lineChart, barChart, table
default: 'table',
},
// 接口请求
request: {
type: Function,
},
// 是否在创建的时候自动调用request接口请求
isCreateRequest: {
type: Boolean,
default: true,
},
// 自定义的查询参数处理方法,可用于添加和修改查询参数
dealQueryFun: {
type: Function,
default: null,
},
// 自定义的返回数据处理方法,可用于添加和修改查询参数
dealDataFun: {
type: Function,
default: null,
},
// 导出接口请求
requestExport: {
type: Function,
default: null,
},
// 导出文件名(需包含后缀名)
exportFileName: { type: String },
// 图表图例; 单个图例可传字符串,多个图例传数组
legend: {
type: [Array, String],
default: null,
},
// 自定义图表配置项,使用loadsh.merge(origin, option)和原有的配置进行覆盖合并
// loadsh.merge: https://www.html.cn/doc/lodash/#_mergeobject-sources
option: {
type: Object,
default: null,
},
},
data() {
return {
loading: false,
// 查询参数
query: {
pageNum: 1,
pageSize: 10,
},
// 表格列表数据,或图表的series数据
data: [],
xData: [],
total: 0,
}
},
computed: {},
watch: {},
created() {
this.isCreateRequest && this.getList()
},
mounted() {},
methods: {
getList(page) {
this.loading = true
if (this.type == 'table') {
page && (this.query.pageNum = page)
}
this.dealQueryFun && this.dealQueryFun(this.query)
let query = JSON.parse(JSON.stringify(this.query))
if (query.dateRange) {
query.beginTime = query.dateRange[0]
query.endTime = query.dateRange[1]
} else {
delete query.beginTime
delete query.endTime
}
delete query.dateRange
return this.request(query)
.then(({ rows, total, data }) => {
data = data || rows
// if (!data) return
if (this.type == 'table') {
// 表格
this.data = data
this.total = +total
this.dealDataFun && this.dealDataFun(data, this.data)
} else {
// 自定义echarts图表数据处理
// this.data = data
this.dealDataFun && this.dealDataFun(data, this.data, this.xData)
}
})
.catch((e) => {
console.error(e)
})
.finally(() => {
this.loading = false
})
},
},
}
</script>
<template>
<div class="RequestChartTable g-card">
<div class="g-card-title" v-if="title">{{ title }}</div>
<div class="RequestChartTable-operation" v-if="requestExport || $slots.query">
<slot name="operation" v-bind="query"></slot>
<el-button
v-if="requestExport"
style="position: absolute; right: 0"
type="primary"
@click="$exportData(requestExport.bind(null, query), exportFileName)">
导出 Excel
</el-button>
</div>
<div v-loading="loading">
<!-- 折线图 -->
<ChartLine v-if="type == 'lineChart'" :xData="xData" :legend="legend" :series="data" :option="option"></ChartLine>
<!-- 柱状图 -->
<ChartBar v-else-if="'barChart'.includes(type)" :xData="xData" :series="data" :option="option"></ChartBar>
<!-- 表格 -->
<template v-else-if="type == 'table'">
<slot :data="data"></slot>
<pagination
:total="total"
v-model:page="query.pageNum"
v-model:limit="query.pageSize"
@pagination="getList()" />
</template>
</div>
</div>
</template>
<style lang="scss" scoped>
.RequestChartTable-operation {
position: relative;
// display: flex;
// align-items: center;
margin-bottom: var(--card-margin, 16px);
}
</style>