VXETable构建强大的表格组件


我们最终实现效果如下:

拿到需求的那一刻,先分析下,里面涉及了自定义单元格样式,合并单元格,自定义表头,动态列…我们一个一个来实现,抽丝剥茧,都不是问题。

基础框架

由于是对年代已久的项目进行迭代更新,所以版本都很低

我们就基于这些开发。

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
<template>
<div class="container">
<vxe-table
class="weighing-table"
border
resizable
height="500"
:data="tableData"
:footer-data="footerData"
show-footer
:merge-footer-items="mergeFooterItems"
:header-cell-class-name="headerCellClassName2"
:footer-cell-class-name="footerCellClassName2"
v-loading="isLoading"
>
<vxe-colgroup field="name" title="" align="center">
<template #header>
<div style="display:flex;padding:20px">
<div style="font-size:18px;margin-right:60px">无 菌 线 调 配 科 称 料 作 业 记 录 表</div>
<div>表 号:ZD/T-001-001</div>
</div>
</template>
<vxe-colgroup field="name">
<template #header>
<div style="display:flex">
<div style="flex:1">班次:</div>
<div style="flex:1">生产品项:</div>
<div style="flex:1">生产日期:</div>
<div style="flex:1">线别:</div>
</div>
</template>
<vxe-column field="seq" type="seq" width="60" align="center"></vxe-column>
<vxe-column width="160">
<template #header>
原粮名称
</template>
<template #default="{ row }">
{{ row.MaterialName }}
</template>
</vxe-column>
<vxe-column field="LotNO" title="生产批号" width="100"></vxe-column>
<vxe-column field="_MfgDate" title="生产日期" width="100"></vxe-column>
<vxe-column field="StandardQuantity" title="标准用量kg" align="center"></vxe-column>
<vxe-colgroup v-for="i in 11" :field="`Quantity-${i}`" :key="i">
<template #header>
<p style="white-space:nowrap;">{{ `第 ${i } 桶` }}</p>
<p> / 锅</p>
</template>
<vxe-column :field="`Quantity${i}`" title="重量kg" align="center"></vxe-column>
</vxe-colgroup>
<vxe-colgroup field="wlgk" title="物料管控" width="60" align="center">
<vxe-column field="TakeOverQuantity" title="上班结存kg" align="center"></vxe-column>
<vxe-column field="ReceiptQuantity" title="本班领用kg" align="center"></vxe-column>
<vxe-column field="HandOverQuantity" title="本班结存kg" align="center"></vxe-column>
<vxe-column field="SumQuantity" title="本班消耗kg" align="center"></vxe-column>
<vxe-column field="TheoreticalQuantity" title="理论消耗kg" align="center"></vxe-column>
<vxe-column field="WastageQuantity" title="损耗kg" align="center"></vxe-column>
</vxe-colgroup>
<vxe-column field="AbnormalAnalysis" title="异常分析" ></vxe-column>
</vxe-colgroup>
</vxe-colgroup>
</vxe-table>
</div>
</template>

<script>
import {data} from "./data.js";

export default {
name: "WeighingTable",
data() {
return {
isLoading: true,
tableData: [],
footerData: [
{ seq: '感官风味确认', TakeOverQuantity: "更换供应商或批次时记录"},
{ seq: '称料员', },
{ seq: '品保稽核', },
{ seq: '上料时间', StandardQuantity: "粉剂",},
{ seq: '', StandardQuantity: "香精" },
{ seq: '', StandardQuantity: "茶叶" },
{ seq: '责任人:',Quantity7:"领班:", },
],
mergeFooterItems: [
{ row: 0, col: 0, rowspan: 1, colspan: 5 },
{ row: 1, col: 0, rowspan: 1, colspan: 5 },
{ row: 2, col: 0, rowspan: 1, colspan: 5 },
{ row: 3, col: 0, rowspan: 3, colspan: 4 },
{ row: 0, col: 16, rowspan: 6, colspan: 7 },
{ row: 6, col: 0, rowspan: 1, colspan: 11 },
{ row: 6, col: 11, rowspan: 1, colspan: 12 },
],
}
},
mounted() {
this.getTableData();
},

methods: {
headerCellClassName2({ $rowIndex, columnIndex }) {
if (columnIndex === 0) {
if ($rowIndex === 1) {
return 'w100';
}
}
},
footerCellClassName2 ({ $rowIndex, columnIndex }) {
if (columnIndex === 0) {
if ($rowIndex === 6) {
return 'text-left';
}
}
},
getTableData() {
if (data?.OrderDetail?.length) {
data?.OrderDetail?.forEach((item,index) => {
this.footerData[0][`Quantity${index}`] = item.IsReviewDone ? '是' : '否';
this.footerData[1][`Quantity${index}`] = item.Operator;
this.footerData[3][`Quantity${index}`] = 'moment(item.ChargeInDateTime1).format("YYYY-MM-DD hh:mm:ss")';
this.footerData[4][`Quantity${index}`] = 'moment(item.ChargeInDateTime2).format("YYYY-MM-DD hh:mm:ss")';
this.footerData[5][`Quantity${index}`] = 'moment(item.ChargeInDateTime3).format("YYYY-MM-DD hh:mm:ss")';
})
}
this.tableData = data.MaterialDetail;
this.isLoading = false;
}
}
}
</script>

核心的部分其实就是

  1. vxe-colgroup: 用于定义列组,他可以将多个列归类在一起

  2. footer-data: 则是定义表尾数据

  3. merge-footer-items:用于定义合并单元格的规则,以便在表格的表尾进行单元格合并

    • row:合并的起始行索引。
    • col:合并的起始列索引。
    • rowspan:合并的行数。
    • colspan:合并的列数。
  4. header-cell-class-name: 则是于为表头单元格定义自定义的 CSS 类名,反之尾部也有

我把demo放在我的码云仓库了,自己动手做是最好的

https://gitee.com/pearpear/excel-table-demo

我的微信公众号: 梨的前端小屋


文章作者: 梨啊梨
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 梨啊梨 !
  目录