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

[Table] Rows are not provided correct $implicit with trackBy #7877

Closed
quanterion opened this issue Oct 18, 2017 · 4 comments · Fixed by #7893
Closed

[Table] Rows are not provided correct $implicit with trackBy #7877

quanterion opened this issue Oct 18, 2017 · 4 comments · Fixed by #7893
Assignees
Labels
P2 The issue is important to a large percentage of users, with a workaround

Comments

@quanterion
Copy link
Contributor

quanterion commented Oct 18, 2017

Bug:

MatTable doesn't refresh row columns if [trackBy] function used. For example if I replace rows array with a new one having the same ids, but completely different data MatTable won't update. I suspect the same applies to CdkTable.
It looks like it correctly updates table when removing / adding rows but it fails to trigger ChangeDetection on components if no rows added or removed

What is the expected behavior?

When used with trackBy function Table should trigger change detection on all existing rows, just like ngFor with trackBy do.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-material2-issue-tbrfqj?file=app%2Fapp.component.html

@quanterion quanterion changed the title bug(table): MatTable don't refresh row columns when used with trackBy bug(table): MatTable don't refresh rows when used with trackBy Oct 18, 2017
@willshowell
Copy link
Contributor

Wouldn't this be what you want?

trackBy is used to uniquely identify rows when you want to use some identifier other than the Object Reference. By using the same id for the swapped row, you're purposefully tricking the table into thinking that nothing has changed.

@quanterion
Copy link
Contributor Author

quanterion commented Oct 18, 2017

I suppose using trackBy is intended to prevent recreating of components and should not stop change detection to happen. I updated stackblitz template to show that ngFor+trackBy updates correctly.

@andrewseguin
Copy link
Contributor

Thanks for reporting this issue. The intention of trackBy is to make sure we are being smart about when to build/teardown rows, which is a separate concern than change detection. In fact, since cell templates are written by the user and not the table, the table has no control over when change detection occurs for those cells.

It turns out that when trackBy is used, a row can be re-used for a different data object, but the table never updates row/cell $implicit data (the exported variable for cells). ngFor does have this built in by checking for identity changes in the data, but it didn't get brought over to the table's logic.

Thanks again for the issue, this was a major one that I'm surprised got this far.

@andrewseguin andrewseguin self-assigned this Oct 18, 2017
@andrewseguin andrewseguin changed the title bug(table): MatTable don't refresh rows when used with trackBy [Table] Rows are not provided correct $implicit with trackBy Oct 19, 2017
@andrewseguin andrewseguin added P2 The issue is important to a large percentage of users, with a workaround and removed mat-table labels Oct 19, 2017
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P2 The issue is important to a large percentage of users, with a workaround
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants