Description
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:
- Check if the sort works
- Click on the button to change the table
- 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