Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Table Performance #9426

Merged
merged 2 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/table/src/layout-observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export default {
created() {
this.tableLayout.addObserver(this);
},

destroyed() {
this.tableLayout.removeObserver(this);
},

computed: {
tableLayout() {
let layout = this.layout;
if (!layout && this.table) {
layout = this.table.layout;
}
if (!layout) {
throw new Error('Can not find table layout.');
}
return layout;
}
},

mounted() {
this.onColumnsChange(this.tableLayout);
this.onScrollableChange(this.tableLayout);
},

updated() {
if (this.__updated__) return;
this.onColumnsChange(this.tableLayout);
this.onScrollableChange(this.tableLayout);
this.__updated__ = true;
},

methods: {
onColumnsChange() {
const cols = this.$el.querySelectorAll('colgroup > col');
if (!cols.length) return;
const flattenColumns = this.tableLayout.getFlattenColumns();
const columnsMap = {};
flattenColumns.forEach((column) => {
columnsMap[column.id] = column;
});
for (let i = 0, j = cols.length; i < j; i++) {
const col = cols[i];
const name = col.getAttribute('name');
const column = columnsMap[name];
if (column) {
col.setAttribute('width', column.realWidth || column.width);
}
}
},

onScrollableChange(layout) {
const cols = this.$el.querySelectorAll('colgroup > col[name=gutter]');
for (let i = 0, j = cols.length; i < j; i++) {
const col = cols[i];
col.setAttribute('width', layout.scrollY ? layout.gutterWidth : '0');
}
const ths = this.$el.querySelectorAll('th.gutter');
for (let i = 0, j = ths.length; i < j; i++) {
const th = ths[i];
th.style.width = layout.scrollY ? layout.gutterWidth + 'px' : '0';
th.style.display = layout.scrollY ? '' : 'none';
}
}
}
};
21 changes: 8 additions & 13 deletions packages/table/src/table-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { hasClass, addClass, removeClass } from 'element-ui/src/utils/dom';
import ElCheckbox from 'element-ui/packages/checkbox';
import ElTooltip from 'element-ui/packages/tooltip';
import debounce from 'throttle-debounce/debounce';
import LayoutObserver from './layout-observer';

export default {
name: 'ElTableBody',

mixins: [LayoutObserver],

components: {
ElCheckbox,
ElTooltip
Expand All @@ -16,9 +21,6 @@ export default {
},
stripe: Boolean,
context: {},
layout: {
required: true
},
rowClassName: [String, Function],
rowStyle: [Object, Function],
fixed: String,
Expand All @@ -35,11 +37,7 @@ export default {
border="0">
<colgroup>
{
this._l(this.columns, column =>
<col
name={ column.id }
width={ column.realWidth || column.width }
/>)
this._l(this.columns, column => <col name={ column.id } />)
}
</colgroup>
<tbody>
Expand Down Expand Up @@ -112,9 +110,6 @@ export default {
}
})
}
{
!this.fixed && this.layout.scrollY && this.layout.gutterWidth ? <td class="gutter" /> : ''
}
</tr>,
this.store.isRowExpanded(row)
? (<tr>
Expand Down Expand Up @@ -344,7 +339,7 @@ export default {

if (hasClass(cellChild, 'el-tooltip') && cellChild.scrollWidth > cellChild.offsetWidth && this.$refs.tooltip) {
const tooltip = this.$refs.tooltip;

// TODO 会引起整个 Table 的重新渲染,需要优化
this.tooltipContent = cell.textContent || cell.innerText;
tooltip.referenceElm = cell;
tooltip.$refs.popper && (tooltip.$refs.popper.style.display = 'none');
Expand All @@ -363,7 +358,7 @@ export default {
const cell = getCell(event);
if (!cell) return;

const oldHoverState = this.table.hoverState;
const oldHoverState = this.table.hoverState || {};
this.table.$emit('cell-mouse-leave', oldHoverState.row, oldHoverState.column, oldHoverState.cell, event);
},

Expand Down
45 changes: 26 additions & 19 deletions packages/table/src/table-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ const DEFAULT_RENDER_CELL = function(h, { row, column }) {
return value;
};

const parseWidth = (width) => {
if (width !== undefined) {
width = parseInt(width, 10);
if (isNaN(width)) {
width = null;
}
}
return width;
};

const parseMinWidth = (minWidth) => {
if (minWidth !== undefined) {
minWidth = parseInt(minWidth, 10);
if (isNaN(minWidth)) {
minWidth = 80;
}
}
return minWidth;
};

export default {
name: 'ElTableColumn',

Expand Down Expand Up @@ -205,25 +225,12 @@ export default {
let parent = this.columnOrTableParent;
let owner = this.owner;
this.isSubColumn = owner !== parent;
this.columnId = (parent.tableId || (parent.columnId + '_')) + 'column_' + columnIdSeed++;
this.columnId = (parent.tableId || parent.columnId) + '_column_' + columnIdSeed++;

let type = this.type;

let width = this.width;
if (width !== undefined) {
width = parseInt(width, 10);
if (isNaN(width)) {
width = null;
}
}

let minWidth = this.minWidth;
if (minWidth !== undefined) {
minWidth = parseInt(minWidth, 10);
if (isNaN(minWidth)) {
minWidth = 80;
}
}
const width = parseWidth(this.width);
const minWidth = parseMinWidth(this.minWidth);

let isColumnGroup = false;

Expand Down Expand Up @@ -353,22 +360,22 @@ export default {

width(newVal) {
if (this.columnConfig) {
this.columnConfig.width = newVal;
this.columnConfig.width = parseWidth(newVal);
this.owner.store.scheduleLayout();
}
},

minWidth(newVal) {
if (this.columnConfig) {
this.columnConfig.minWidth = newVal;
this.columnConfig.minWidth = parseMinWidth(newVal);
this.owner.store.scheduleLayout();
}
},

fixed(newVal) {
if (this.columnConfig) {
this.columnConfig.fixed = newVal;
this.owner.store.scheduleLayout();
this.owner.store.scheduleLayout(true);
}
},

Expand Down
27 changes: 12 additions & 15 deletions packages/table/src/table-footer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import LayoutObserver from './layout-observer';

export default {
name: 'ElTableFooter',

mixins: [LayoutObserver],

render(h) {
const sums = [];
this.columns.forEach((column, index) => {
Expand Down Expand Up @@ -41,16 +45,10 @@ export default {
border="0">
<colgroup>
{
this._l(this.columns, column =>
<col
name={ column.id }
width={ column.realWidth || column.width }
/>)
this._l(this.columns, column => <col name={ column.id } />)
}
{
!this.fixed && this.layout.gutterWidth
? <col name="gutter" width={ this.layout.scrollY ? this.layout.gutterWidth : '' }></col>
: ''
this.hasGutter ? <col name="gutter" /> : ''
}
</colgroup>
<tbody class={ [{ 'has-gutter': this.hasGutter }] }>
Expand All @@ -70,9 +68,7 @@ export default {
)
}
{
this.hasGutter
? <td class="gutter" style={{ width: this.layout.scrollY ? this.layout.gutterWidth + 'px' : '0' }}></td>
: ''
this.hasGutter ? <th class="gutter"></th> : ''
}
</tr>
</tbody>
Expand All @@ -85,9 +81,6 @@ export default {
store: {
required: true
},
layout: {
required: true
},
summaryMethod: Function,
sumText: String,
border: Boolean,
Expand All @@ -103,6 +96,10 @@ export default {
},

computed: {
table() {
return this.$parent;
},

isAllSelected() {
return this.store.states.isAllSelected;
},
Expand All @@ -124,7 +121,7 @@ export default {
},

hasGutter() {
return !this.fixed && this.layout.gutterWidth;
return !this.fixed && this.tableLayout.gutterWidth;
}
},

Expand Down
26 changes: 7 additions & 19 deletions packages/table/src/table-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ElCheckbox from 'element-ui/packages/checkbox';
import ElTag from 'element-ui/packages/tag';
import Vue from 'vue';
import FilterPanel from './filter-panel.vue';
import LayoutObserver from './layout-observer';

const getAllColumns = (columns) => {
const result = [];
Expand Down Expand Up @@ -65,13 +66,14 @@ const convertToRows = (originColumns) => {
export default {
name: 'ElTableHeader',

mixins: [LayoutObserver],

render(h) {
const originColumns = this.store.states.originColumns;
const columnRows = convertToRows(originColumns, this.columns);
// 是否拥有多级表头
const isGroup = columnRows.length > 1;
if (isGroup) this.$parent.isGroup = true;

return (
<table
class="el-table__header"
Expand All @@ -80,16 +82,10 @@ export default {
border="0">
<colgroup>
{
this._l(this.columns, column =>
<col
name={ column.id }
width={ column.realWidth || column.width }
/>)
this._l(this.columns, column => <col name={ column.id } />)
}
{
!this.fixed && this.layout.gutterWidth
? <col name="gutter" width={ this.layout.scrollY ? this.layout.gutterWidth : '' }></col>
: ''
this.hasGutter ? <col name="gutter" /> : ''
}
</colgroup>
<thead class={ [{ 'is-group': isGroup, 'has-gutter': this.hasGutter }] }>
Expand Down Expand Up @@ -137,12 +133,7 @@ export default {
)
}
{
this.hasGutter
? <th class="gutter" style={{
width: this.layout.scrollY ? this.layout.gutterWidth + 'px' : '0',
display: this.layout.scrollY ? '' : 'none'
}}></th>
: ''
this.hasGutter ? <th class="gutter"></th> : ''
}
</tr>
)
Expand All @@ -157,9 +148,6 @@ export default {
store: {
required: true
},
layout: {
required: true
},
border: Boolean,
defaultSort: {
type: Object,
Expand Down Expand Up @@ -211,7 +199,7 @@ export default {
},

hasGutter() {
return !this.fixed && this.layout.gutterWidth;
return !this.fixed && this.tableLayout.gutterWidth;
}
},

Expand Down
Loading