Skip to content

perf(table): Reduce calls to updateStickyColumnStyles #19739

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

Merged
merged 2 commits into from
Jul 29, 2020
Merged
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
27 changes: 18 additions & 9 deletions src/cdk/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
// this setter will be invoked before the row outlet has been defined hence the null check.
if (this._rowOutlet && this._rowOutlet.viewContainer.length) {
this._forceRenderDataRows();
this.updateStickyColumnStyles();
}
}
_multiTemplateDataRows: boolean = false;
Expand Down Expand Up @@ -459,7 +460,9 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
}

// Render updates if the list of columns have been changed for the header, row, or footer defs.
this._renderUpdatedColumns();
const columnsChanged = this._renderUpdatedColumns();
const stickyColumnStyleUpdateNeeded =
columnsChanged || this._headerRowDefChanged || this._footerRowDefChanged;

// If the header row definition has been changed, trigger a render to the header row.
if (this._headerRowDefChanged) {
Expand All @@ -477,6 +480,10 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
// connection has already been made.
if (this.dataSource && this._rowDefs.length > 0 && !this._renderChangeSubscription) {
this._observeRenderChanges();
} else if (stickyColumnStyleUpdateNeeded) {
// In the above case, _observeRenderChanges will result in updateStickyColumnStyles being
// called when it row data arrives. Otherwise, we need to call it proactively.
this.updateStickyColumnStyles();
}

this._checkStickyStates();
Expand Down Expand Up @@ -783,22 +790,27 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
* whether the sticky states have changed for the header or footer. If there is a diff, then
* re-render that section.
*/
private _renderUpdatedColumns() {
private _renderUpdatedColumns(): boolean {
const columnsDiffReducer = (acc: boolean, def: BaseRowDef) => acc || !!def.getColumnsDiff();

// Force re-render data rows if the list of column definitions have changed.
if (this._rowDefs.reduce(columnsDiffReducer, false)) {
const dataColumnsChanged = this._rowDefs.reduce(columnsDiffReducer, false);
if (dataColumnsChanged) {
this._forceRenderDataRows();
}

// Force re-render header/footer rows if the list of column definitions have changed..
if (this._headerRowDefs.reduce(columnsDiffReducer, false)) {
// Force re-render header/footer rows if the list of column definitions have changed.
const headerColumnsChanged = this._headerRowDefs.reduce(columnsDiffReducer, false);
if (headerColumnsChanged) {
this._forceRenderHeaderRows();
}

if (this._footerRowDefs.reduce(columnsDiffReducer, false)) {
const footerColumnsChanged = this._footerRowDefs.reduce(columnsDiffReducer, false);
if (footerColumnsChanged) {
this._forceRenderFooterRows();
}

return dataColumnsChanged || headerColumnsChanged || footerColumnsChanged;
}

/**
Expand Down Expand Up @@ -868,7 +880,6 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes

this._headerRowDefs.forEach((def, i) => this._renderRow(this._headerRowOutlet, def, i));
this.updateStickyHeaderRowStyles();
this.updateStickyColumnStyles();
}
/**
* Clears any existing content in the footer row outlet and creates a new embedded view
Expand All @@ -882,7 +893,6 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes

this._footerRowDefs.forEach((def, i) => this._renderRow(this._footerRowOutlet, def, i));
this.updateStickyFooterRowStyles();
this.updateStickyColumnStyles();
}

/** Adds the sticky column styles for the rows according to the columns' stick states. */
Expand Down Expand Up @@ -1042,7 +1052,6 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
this._dataDiffer.diff([]);
this._rowOutlet.viewContainer.clear();
this.renderRows();
this.updateStickyColumnStyles();
}

/**
Expand Down