基于elementUI封装的表格分页一体组件【拿来即用】

内容说明

  1. 表格
  2. 表格左上角插槽
  3. 表格右上角插槽
  4. 分页
  5. 预设了导出、打印事件

拆箱即用

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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<template>
<div class="">
<div class="d-flex justify-content-between align-items-end">
<el-button-group class="d-flex functions">
<slot name="left"></slot>
<el-dropdown :disabled="data.length < 1" v-if="derive && (total > pageSize)" trigger="click" @command="exporting">
<el-button :size="size">
导出<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="1">导出本页</el-dropdown-item>
<el-dropdown-item command="2">导出全部</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
<el-button v-if="derive && (total < pageSize)" :disabled="data.length < 1" :size="size"
@click="exporting('1')">导出</el-button>
<el-dropdown :disabled="data.length < 1" v-if="$listeners.printing && (total > pageSize)" trigger="click"
@command="printing">
<el-button :size="size">
打印<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="1">打印本页</el-dropdown-item>
<el-dropdown-item command="2">打印全部</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
<el-button v-if="$listeners.printing && (total < pageSize)" :disabled="data.length < 1" :size="size"
@click="printing('1')">打印</el-button>
</el-button-group>
<el-button-group>
<slot name="right"></slot>
</el-button-group>
</div>
<el-table ref="table" :row-class-name="rowClassName" :size="size" :data="data"
:[olyHeight]="height || maxHeight || '400'" :border="border" :row-key="rowKey" :empty-text="emptyText"
v-loading="loading" :show-summary="showSummary" :summary-method="summaryMethod" tooltip-effect="dark"
highlight-current-row @row-click="rowClick" @cell-click="cellClick" @select="select" @select-all="selectAll"
@selection-change="selectionChange" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
element-loading-background="rgba(0, 0, 0, 0.8)" @row-dblclick="rowDblclick">
<el-table-column v-if="showSelection" fixed="left" :reserve-selection="reserveSelection" type="selection"
min-width="60" :selectable="selecteisDisabled">
</el-table-column>
<el-table-column v-if="showIndex" fixed="left" label="序号" min-width="50">
<template slot-scope="scope">
{{ pageSize * (currentPage - 1) + (scope.$index + 1) }}
</template>
</el-table-column>
<slot name="columns"></slot>
</el-table>
<el-pagination class="text-end mt-3" v-if="$listeners['current-change']" @size-change="dataSizeChange"
@current-change="handleCurrentChange" :page-sizes="[$tableDataSize, 20, 30, 50]" :page-size="pageSize"
:disabled="total <= 0 || loading" :current-page="currentPage" layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>
</template>

<script>
/**
* @author 全易
* @time 2021-12-28 13:26:08 星期二
* @description 自定义的表格,使用方式同elementUI。属性、方法、事件参考elementUI文档
**/
import { exportExcel } from "@/utils/export-file.js";

export default {
name: "custom-table",
data() {
return {
pageNow: 1
};
},
props: {
name: {
type: String,
},
// 显示序号列
showIndex: {
type: Boolean,
default: false,
},
data: {
type: Array,
default: () => {
return [];
},
},
loading: {
type: Boolean,
default: false,
},
size: {
type: String,
default: "small",
},
emptyText: {
type: String,
},
border: {
type: Boolean,
default: true,
},
rowKey: {
type: [String, Number],
},
height: {
type: [String, Number],
},
maxHeight: {
type: [String, Number],
},
total: {
type: Number,
default: 0,
},
pageSize: {
type: Number,
default() {
return this.$tableDataSize;
},
},
currentPage: {
type: Number,
default() {
return 1;
},
},
showSummary: {
type: Boolean,
default: false,
},
summaryMethod: {
type: Function
},
reserveSelection: {
type: Boolean,
default: true
},
// 返回值用来决定这一行的 CheckBox 是否可以勾选
selecteisDisabled: {
type: Function
},
rowClassName: {
type: Function
},
derive: {
type: Function,
default: null
}
},
watch: {
data(now, old) {
const that = this;
this.$nextTick(() => {
that.doLayout(); // 为了解决行错位问题
});
},
},
computed: {
showSelection() {
return this.$listeners["select"] || this.$listeners["selection-change"];
},
olyHeight() {
// 因为elementUI不能 height 和 max-height 同时存在,所以采用动态属性
// console.log("height:", this.height, "maxHeight:", this.maxHeight);
return this.height ? "height" : "max-height";
},
},
activated() {
this.doLayout();
},
methods: {
// 页量
dataSizeChange(val) {
console.log(`每页 ${val} 条`);
this.$emit("size-change", val);
},
// 翻页
handleCurrentChange(val) {
console.log(`当前页: ${val}`);
this.$emit("current-change", val);
},
// 选择数据
select(selection, row) {
// console.log(election, row);
this.$emit("select", selection, row);
this.doLayout();
},
// 选择数据
selectionChange(selection) {
// console.log(selection);
this.$emit("selection-change", selection);
this.doLayout();
},
// 全选数据
selectAll(selection) {
console.log(selection);
this.$emit("select-all", selection);
this.doLayout();
},
// 清空选择
clearSelection() {
this.$refs.table.clearSelection();
this.doLayout();
},
// 全选
toggleAllSelection() {
this.$refs.table.toggleAllSelection();
this.doLayout();
},
// 选择数据
toggleRowSelection(row = [], selected = true) {
this.$refs.table.toggleRowSelection(row, selected);
this.doLayout();
},
// 单选数据
setCurrentRow(row) {
this.$refs.table.setCurrentRow(row);
this.doLayout();
},
// 点击行
rowClick(row, column, event) {
// console.log(row, column, event);
this.$emit("row-click", row, column, event);
this.doLayout();
},
// 点击单元格
cellClick(row, column, cell, event) {
this.$emit("cell-click", row, column, cell, event);
this.doLayout();
},
// 双击行
rowDblclick(row, column, event) {
this.$emit("row-dblclick", row, column, event);
},
// 导出表格
exporting(command) {
if (this.total < 1) {
this.$message.warning("无数据可导");
return false;
}

if (command === "1" || (command === "2" && this.total < 3000)) {
this.exportingExcel(command);
} else {
this.$message.success("即将打开微信对话,请注意您的微信软件")
window.open(process.env.VUE_APP_DEPARTMENT_SERVICE, "_blank", "location=no, status=no, menubar=no");
}

this.doLayout();
},
// 执行导出表格
exportingExcel(command) {
const title = this.name || this.$route.meta.menuName + "表";
const notify = Notification.permission === "granted" ? new Notification("开始下载", { body: title }) : this.$notify.info({ title: "开始下载" });

this.derive(command).then(data => {
if (data) {
exportExcel(data, title);
Notification.permission === "granted" ? new Notification("下载成功", { body: title }) : this.$notify.success({ title: "下载成功" });
} else {
Notification.permission === "granted" ? new Notification("下载失败", { body: title }) : this.$notify.error({ title: "下载失败" });
}
}).catch(() => {
Notification.permission === "granted" ? new Notification("下载失败", { body: title }) : this.$notify.error({ title: "下载失败" });
}).finally(() => {
notify.close();
})
},
// 打印表格数据
printing(command) {
if (this.total < 1) {
return this.$message.info("无数据打印");
}

this.$emit("printing", command);
this.doLayout();
},
// 重载表格
doLayout() {
this.$refs.table.doLayout();
},
// 获取列
getColumns() {
let columns = [];
(function flattening(data) {
data.map((item) => {
if (item.$children) {
flattening(item.$children); // 递归执行
}
if (item.label && item.prop) {
columns.push({
label: (item.$parent.label || "") + item.label,
prop: item.prop,
});
}
});
})(this.$refs.table.$children);
// console.log(columns);
return columns;
},
exportExcel
},
};
</script>

<style lang="less" scoped>

</style>

基于elementUI封装的表格分页一体组件【拿来即用】
https://github.com/chergn/chergn.github.io/11e6391aeca3/
作者
全易
发布于
2024年3月28日
许可协议