Skip to content

Commit 2b9be3c

Browse files
jikkaiLeopoldthecoder
authored andcommitted
Table: improve performance of summary-method (#11521)
1 parent d1391e3 commit 2b9be3c

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

packages/table/src/table-footer.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,40 @@ export default {
66
mixins: [LayoutObserver],
77

88
render(h) {
9-
const sums = [];
10-
this.columns.forEach((column, index) => {
11-
if (index === 0) {
12-
sums[index] = this.sumText;
13-
return;
14-
}
15-
const values = this.store.states.data.map(item => Number(item[column.property]));
16-
const precisions = [];
17-
let notNumber = true;
18-
values.forEach(value => {
19-
if (!isNaN(value)) {
20-
notNumber = false;
21-
let decimal = ('' + value).split('.')[1];
22-
precisions.push(decimal ? decimal.length : 0);
9+
let sums = [];
10+
if (this.summaryMethod) {
11+
sums = this.summaryMethod({ columns: this.columns, data: this.store.states.data });
12+
} else {
13+
this.columns.forEach((column, index) => {
14+
if (index === 0) {
15+
sums[index] = this.sumText;
16+
return;
2317
}
24-
});
25-
const precision = Math.max.apply(null, precisions);
26-
if (!notNumber) {
27-
sums[index] = values.reduce((prev, curr) => {
28-
const value = Number(curr);
18+
const values = this.store.states.data.map(item => Number(item[column.property]));
19+
const precisions = [];
20+
let notNumber = true;
21+
values.forEach(value => {
2922
if (!isNaN(value)) {
30-
return parseFloat((prev + curr).toFixed(Math.min(precision, 20)));
31-
} else {
32-
return prev;
23+
notNumber = false;
24+
let decimal = ('' + value).split('.')[1];
25+
precisions.push(decimal ? decimal.length : 0);
3326
}
34-
}, 0);
35-
} else {
36-
sums[index] = '';
37-
}
38-
});
27+
});
28+
const precision = Math.max.apply(null, precisions);
29+
if (!notNumber) {
30+
sums[index] = values.reduce((prev, curr) => {
31+
const value = Number(curr);
32+
if (!isNaN(value)) {
33+
return parseFloat((prev + curr).toFixed(Math.min(precision, 20)));
34+
} else {
35+
return prev;
36+
}
37+
}, 0);
38+
} else {
39+
sums[index] = '';
40+
}
41+
});
42+
}
3943

4044
return (
4145
<table
@@ -61,7 +65,7 @@ export default {
6165
class={ [column.id, column.headerAlign, column.className || '', this.isCellHidden(cellIndex, this.columns) ? 'is-hidden' : '', !column.children ? 'is-leaf' : '', column.labelClassName] }>
6266
<div class={ ['cell', column.labelClassName] }>
6367
{
64-
this.summaryMethod ? this.summaryMethod({ columns: this.columns, data: this.store.states.data })[cellIndex] : sums[cellIndex]
68+
sums[cellIndex]
6569
}
6670
</div>
6771
</td>

0 commit comments

Comments
 (0)