Skip to content

fix(cdk/table): ensure CdkTable updates view with OnPush and trackBy #31451

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

skrtheboss
Copy link
Contributor

Fixes #24483

Description

When CdkTable is used with the OnPush change detection strategy and a trackBy function, changes to the dataSource might not trigger a view update if the trackBy function indicates that the row instances themselves haven't changed. This can lead to the data-bound elements within the table rows displaying outdated information, even if their underlying data properties have been modified.

This PR addresses the issue by adding _changeDetectorRef.markForCheck() calls within the renderRows method of CdkTable. These calls ensure that whenever the table's data or row definitions are updated, the component is explicitly marked for change detection. This forces a re-evaluation of the bindings for all visible rows, ensuring the view remains consistent with the latest underlying data.

A new test case has been added to table.spec.ts to specifically verify this behavior in an OnPush component using trackBy when data properties are updated.

When CdkTable is used with OnPush change detection and a `trackBy` function,
changes to the `dataSource` might not trigger a view update if the `trackBy`
function indicates that the rows themselves haven't changed (even if their
internal data has). This can leave the data-bound elements within the
table rows displaying outdated information.

This commit adds `markForCheck()` calls within the `_renderRows` method
of `CdkTable`. These calls ensure that when the table's data is updated
(even without full row re-creation due to `trackBy`), the component is
explicitly marked for change detection. This triggers a re-evaluation
of the bindings for all visible rows, ensuring the view remains
consistent with the underlying data.

Fixes angular#24483
@skrtheboss skrtheboss requested a review from a team as a code owner June 26, 2025 13:30
@skrtheboss skrtheboss requested review from mmalerba and wagnermaciel and removed request for a team June 26, 2025 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(mat-table): MatTable with observable as data source should trigger change detection after update
1 participant