Skip to content

MatTableDataSource does not unsubscribe from sort and paginator streams #21270

Closed
@gaspaonrocks

Description

@gaspaonrocks

What are you trying to do?

I have a dynamic Mat Table with data changing. I want to be able to sort the rows using the Mat Sort directive. However, after the datasource has changed, if I set the sort property with the existing @ViewChild(MatSort), the subsequent calls to sortData increases. It can harm performances if we go back and forth between data sources.

What troubleshooting steps have you tried?

I tried to look in several attributes in MatSort and MatTable and see if I can either destroy the directive or unsubscribe from any Subject. I'm still looking into it, but I'm open to suggestions...

Reproduction

Minimal example here => https://stackblitz.com/edit/angular-ivy-pvqd1r?file=src/app/hello.component.ts

The "randomize" button will switch between 2 table, however, we lose the sort ability. I added the sortData and the sortAccessorKey to add a console.log() to monitor the number of calls.

Steps to reproduce:

  1. Check if the sort works
  2. Click on the button to change the table
  3. See that the sort call increments by one
    repeat steps 2-3 ad nauseum.

If I only changed the table in the randomize method (commenting l31), we lose the sort ability, yet the sortData method is called.

If the directive is destroyed (in my case is to navigate to a completely different component and come back) the MatSort is reset and we are back to square one.

Environment

  • Angular: ~10.2.0
  • CDK/Material: ^10.2.7
  • Browser(s): Firefox
  • Operating System: Windows 10

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: cdk/tablehelp wantedThe team would appreciate a PR from the community to address this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions