I have a page with a Material table. The table has a custom data source since pagination is server side. It also has a check box column. The rows in the table are expandable.
Problem: The expandable rows work fine until I go to some other page and reroute to the above page again. When I reroute back to the page many rows are in expanded state automatically.
I found one question with a similar issue : Similar problem However I don't have tabs so the solution mentioned can't be applied to my problem and fundamentally I am not sure why the solution works.
Code snippet which adds the expandable row to the table:
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
<div [@detailExpand]="element === expandedElement ? 'expanded' : 'collapsed'">
<div>Expandable row details go here</div>
</div>
</td>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns; sticky:true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"
(click)="toggle(row); expandedElement = expandedElement == row ? null : row"
[class.element-expanded-row]="expandedElement == element" class="clickable-row element-row">
</mat-row>
<mat-row *matRowDef="let row; columns: ['expandedDetail'];"
[@detailExpand]="row === expandedElement ? 'expanded' : 'collapsed'">
</mat-row>
</mat-table>
In the TS file I have a variable which I reset to null on ngOnInit:
expandedElement: any;
Also, One more behavior that I observed when I come back to the page was that I have to click the row twice to close it which means the condition to collapse and open the row (element === expandedElement) is satisfied just that the row is visible when I come back to the page.
Does any one has any clue what may be the problem?
Edit :
The expandable table example on angular materials page doesn't have to use [@detailExpand] for the expand detail mat-row however I have to use otherwise I see a empty row in between 2 rows of data. Why so? Does it have to do anything with this problem?
<mat-row *matRowDef="let row; columns: ['expandedDetail'];"
[@detailExpand]="row === expandedElement ? 'expanded' : 'collapsed'">
</mat-row>