TableOrderTotal.vue
6.95 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
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
<template>
<a-card :bordered="false">
<a-row>
<a-col>
<a-switch v-bind="pageSwitchProps" v-model="pageSwitch"/>
</a-col>
<a-col>
<a-table
v-bind="tableProps"
@change="handleTableChange"
style="margin-top: 20px"
>
</a-table>
</a-col>
</a-row>
</a-card>
</template>
<script>
export default {
name: 'TableTotal',
data() {
return {
columns: [
{
title: '#',
width: '180px',
align: 'center',
dataIndex: 'rowIndex',
customRender: function (text, r, index) {
return (text !== '总计') ? (parseInt(index) + 1) : text
}
},
{
title: '姓名',
dataIndex: 'name',
width: 180,
},
{
title: '贡献点',
dataIndex: 'point',
width: 180,
},
{
title: '等级',
dataIndex: 'level',
width: 180,
},
{
title: this.$t('system.updateTime'),
dataIndex: 'updateTime',
width: 180,
},
],
/* 分页参数 */
ipagination: {
current: 1,
pageSize: 10,
},
dataSource: [
{id: "1", name: '张三', point: 23, level: 3, updateTime: '2019-8-14'},
{name: '小王', point: 6, level: 1, updateTime: '2019-8-13'},
{name: '李四', point: 53, level: 8, updateTime: '2019-8-12'},
{name: '小红', point: 44, level: 5, updateTime: '2019-8-11'},
{name: '王五', point: 97, level: 10, updateTime: '2019-8-10'},
{name: '小明', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '小张', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '小六', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '小五', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '小赵', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '李华', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '小康', point: 33, level: 2, updateTime: '2019-8-10'},
{name: '小鹿', point: 33, level: 2, updateTime: '2019-8-10'},
],
newArr: [],
newDataSource: [],
footerDataSource: [],
pageSwitch: true
}
},
computed: {
// 数据表格的固定属性
tableProps() {
let tableProps = {
size: 'middle',
rowKey: 'rowIndex',
columns: this.columns,
scroll: {x: true},
}
let renderFooter = this.footerDataSource.length === 0 ? null : () => this.renderTableFooter(tableProps)
return {
...tableProps,
ref: 'table',
class: 'chart-data-list',
pagination: this.pageSwitch ? this.ipagination : false,
columns: this.columns,
dataSource: this.dataSource,
footer: renderFooter,
}
},
pageSwitchProps() {
return {
checkedChildren: '分页',
unCheckedChildren: '分页',
style: {
position: 'absolute',
right: '0px',
top: '-10px'
}
}
},
},
mounted() {
// this.tableAddTotalRow(this.columns, this.dataSource)
/*新增分页合计方法*/
this.newDataSource = this.dataSource
this.dataHandling(1, this.ipagination.pageSize)
},
watch: {
//update-begin---author:wangshuai ---date:20220209 for:[JTC-494]常用示例->表格合计写法改成新的写法------------
'pageSwitch': function (val) {
if (!val) {
this.dataHandling('-1', 0)
} else {
this.dataHandling(1, this.ipagination.pageSize)
}
},
'ipagination.current': function (val) {
this.dataHandling(val, this.ipagination.pageSize)
},
//当合计行变化时,绑定滚动条
'footerDataSource': {
async handler(dataSource) {
// 当底部合计行有值,并且显示出来时,再同步滚动条
if (dataSource && dataSource.length > 0) {
await this.$nextTick()
// 同步表与footer滚动
let dom = this.$refs.table.$el.querySelectorAll('.ant-table-body')[0]
let footerDom = this.$refs.footerTable.$el.querySelectorAll('.ant-table-body')[0]
dom.addEventListener(
'scroll',
() => {
footerDom.scrollLeft = dom.scrollLeft
},
true,
)
}
},
//update-end---author:wangshuai ---date:20220209 for:[JTC-494]常用示例->表格合计写法改成新的写法------------
}
},
methods: {
/** 表格增加合计行 */
tableAddTotalRow(columns, dataSource) {
let numKey = 'rowIndex'
let totalRow = {[numKey]: '合计'}
columns.forEach(column => {
let {key, dataIndex} = column
if (![key, dataIndex].includes(numKey)) {
let total = 0
dataSource.forEach(data => {
total += /^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseInt(data[dataIndex]) : Number.NaN
console.log(data[dataIndex], ':', (/^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseInt(data[dataIndex]) : Number.NaN))
})
if (Number.isNaN(total)) {
total = '-'
}
totalRow[dataIndex] = total
}
})
dataSource.push(totalRow)
},
handleTableChange(pagination, filters, sorter) {
this.ipagination = pagination;
},
//update-begin---author:wangshuai ---date:20220209 for:[JTC-494]常用示例->表格合计写法改成新的写法------------
/*如果分页走这个方法*/
dataHandling(pageNo, pageSize) {
//根据当前页数和每页显示条数分割数组
let arrs = [];
//如果pageNo不是-1(不分页),那么需要对数据进行分页计算
if (pageNo != -1) {
arrs = this.newDataSource.slice((pageNo - 1) * pageSize, pageNo * pageSize)
} else {
arrs = this.newDataSource
}
let newDataSource = [];
let newArr = {};
newArr.rowIndex = "总计"
let level = 0;
let point = 0;
//每一项的数值相加
for (let j = 0; j < arrs.length; j++) {
level += arrs[j].level;
point += arrs[j].point;
}
newArr.level = level;
newArr.point = point;
newDataSource.push(newArr);
//给foot底部数组赋值
this.footerDataSource = newDataSource;
},
// 渲染表格底部合计行
renderTableFooter(tableProps) {
let h = this.$createElement
return h('a-table', {
ref: 'footerTable',
props: {
...tableProps,
pagination: false,
dataSource: this.footerDataSource,
showHeader: false,
},
})
//update-end---author:wangshuai ---date:20220209 for:[JTC-494]常用示例->表格合计写法改成新的写法------------
}
}
}
</script>
<style scoped lang="less">
/deep/ .chart-data-list .ant-table-footer .ant-table-body {
overflow: hidden !important;
}
/deep/ .ant-table-footer {
padding: 0;
}
</style>