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
import LayoutObserver from './layout-observer';
import { mapStates } from './store/helper';
/**
* 字符串是否含有html标签的检测
* @param htmlStr
*/
function checkHtml (htmlStr) {
let reg = /<[^>]+>/g;
return reg.test(htmlStr);
}
export default {
name: 'ElTableFooter',
mixins: [LayoutObserver],
render(h) {
let sums = [];
if (this.summaryMethod) {
sums = this.summaryMethod({ columns: this.columns, data: this.store.states.data });
} else {
this.columns.forEach((column, index) => {
if (index === 0) {
sums[index] = this.sumText;
return;
}
const values = this.store.states.data.map(item => Number(item[column.property]));
const precisions = [];
let notNumber = true;
values.forEach(value => {
if (!isNaN(value)) {
notNumber = false;
let decimal = ('' + value).split('.')[1];
precisions.push(decimal ? decimal.length : 0);
}
});
const precision = Math.max.apply(null, precisions);
if (!notNumber) {
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr);
if (!isNaN(value)) {
return parseFloat((prev + curr).toFixed(Math.min(precision, 20)));
} else {
return prev;
}
}, 0);
} else {
sums[index] = '';
}
});
}
return (
<table
class="el-table__footer"
cellspacing="0"
cellpadding="0"
border="0">
<colgroup>
{
this.columns.map(column => <col name={ column.id } key={column.id} />)
}
{
this.hasGutter ? <col name="gutter" /> : ''
}
</colgroup>
<tbody class={ [{ 'has-gutter': this.hasGutter }] }>
{
sums.map((itemSum, index) =>
<tr class={ index === sums.length - 1 ? '' : 'lineFooter' }>
{
this.columns.map((column, cellIndex) => <td
key={cellIndex}
colSpan={column.colSpan}
rowSpan={column.rowSpan}
class={this.getRowClasses(column, cellIndex)}>
{
checkHtml(itemSum[cellIndex])
? h(
'div',
{
class: ['cell', column.labelClassName],
// DOM 属性
domProps: {
innerHTML: itemSum[cellIndex]
},
},
[]
)
: <div class={['cell', column.labelClassName]}>{itemSum[cellIndex]}</div>
}
</td>)
}
{
this.hasGutter ? <th class="gutter"></th> : ''
}
</tr>
)
}
</tbody>
</table>
);
},
props: {
fixed: String,
store: {
required: true
},
summaryMethod: Function,
sumText: String,
border: Boolean,
defaultSort: {
type: Object,
default() {
return {
prop: '',
order: ''
};
}
}
},
computed: {
table() {
return this.$parent;
},
hasGutter() {
return !this.fixed && this.tableLayout.gutterWidth;
},
...mapStates({
columns: 'columns',
isAllSelected: 'isAllSelected',
leftFixedLeafCount: 'fixedLeafColumnsLength',
rightFixedLeafCount: 'rightFixedLeafColumnsLength',
columnsCount: states => states.columns.length,
leftFixedCount: states => states.fixedColumns.length,
rightFixedCount: states => states.rightFixedColumns.length
})
},
methods: {
isCellHidden(index, columns, column) {
if (this.fixed === true || this.fixed === 'left') {
return index >= this.leftFixedLeafCount;
} else if (this.fixed === 'right') {
let before = 0;
for (let i = 0; i < index; i++) {
before += columns[i].colSpan;
}
return before < this.columnsCount - this.rightFixedLeafCount;
} else if (!this.fixed && column.fixed) { // hide cell when footer instance is not fixed and column is fixed
return true;
} else {
return (index < this.leftFixedCount) || (index >= this.columnsCount - this.rightFixedCount);
}
},
getRowClasses(column, cellIndex) {
const classes = [column.id, column.align, column.labelClassName];
if (column.className) {
classes.push(column.className);
}
if (this.isCellHidden(cellIndex, this.columns, column)) {
classes.push('is-hidden');
}
if (!column.children) {
classes.push('is-leaf');
}
return classes;
}
}
};