Commit 2b9be3c0 authored by Jikkai Xiao's avatar Jikkai Xiao Committed by 杨奕

Table: improve performance of summary-method (#11521)

parent d1391e32
...@@ -6,36 +6,40 @@ export default { ...@@ -6,36 +6,40 @@ export default {
mixins: [LayoutObserver], mixins: [LayoutObserver],
render(h) { render(h) {
const sums = []; let sums = [];
this.columns.forEach((column, index) => { if (this.summaryMethod) {
if (index === 0) { sums = this.summaryMethod({ columns: this.columns, data: this.store.states.data });
sums[index] = this.sumText; } else {
return; this.columns.forEach((column, index) => {
} if (index === 0) {
const values = this.store.states.data.map(item => Number(item[column.property])); sums[index] = this.sumText;
const precisions = []; return;
let notNumber = true;
values.forEach(value => {
if (!isNaN(value)) {
notNumber = false;
let decimal = ('' + value).split('.')[1];
precisions.push(decimal ? decimal.length : 0);
} }
}); const values = this.store.states.data.map(item => Number(item[column.property]));
const precision = Math.max.apply(null, precisions); const precisions = [];
if (!notNumber) { let notNumber = true;
sums[index] = values.reduce((prev, curr) => { values.forEach(value => {
const value = Number(curr);
if (!isNaN(value)) { if (!isNaN(value)) {
return parseFloat((prev + curr).toFixed(Math.min(precision, 20))); notNumber = false;
} else { let decimal = ('' + value).split('.')[1];
return prev; precisions.push(decimal ? decimal.length : 0);
} }
}, 0); });
} else { const precision = Math.max.apply(null, precisions);
sums[index] = ''; 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 ( return (
<table <table
...@@ -61,7 +65,7 @@ export default { ...@@ -61,7 +65,7 @@ export default {
class={ [column.id, column.headerAlign, column.className || '', this.isCellHidden(cellIndex, this.columns) ? 'is-hidden' : '', !column.children ? 'is-leaf' : '', column.labelClassName] }> class={ [column.id, column.headerAlign, column.className || '', this.isCellHidden(cellIndex, this.columns) ? 'is-hidden' : '', !column.children ? 'is-leaf' : '', column.labelClassName] }>
<div class={ ['cell', column.labelClassName] }> <div class={ ['cell', column.labelClassName] }>
{ {
this.summaryMethod ? this.summaryMethod({ columns: this.columns, data: this.store.states.data })[cellIndex] : sums[cellIndex] sums[cellIndex]
} }
</div> </div>
</td> </td>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment