问题
I have an Angular material table with expandable rows. In every row, I have multiple tabs, On the first tab, I have another table with expandable rows. At the start al the rows are collapsed, so far so good. When I click a row, the row is expanded, again so far, so good. When I change tabs and come back, all my rows are expanded. How can I solve this issue? The HTML code is like Angular material describes on their page for a table with expandable rows.
I created a StackBlitz with my problem, it is a less complicated file then what I'm having, but it has the same problem.
Result:
https://angular-wat7fa.stackblitz.io
Code:
https://stackblitz.com/edit/angular-wat7fa-wzk7sh?file=app/table-expandable-rows-example.html
After I switched tabs and return to the Messages tab
Part of my HTML file
<table
mat-table
[dataSource]="messages.results"
multiTemplateDataRows
class="mat-elevation-z0 messages-table"
>
<ng-container matColumnDef="enqueuedTimeUtc">
All my columns
</ng-container>
<ng-container matColumnDef="info">
<th mat-header-cell *matHeaderCellDef>Info</th>
<td mat-cell *matCellDef="let context">
<i
[appTooltip]="customerInfo"
[appTooltipContext]="context"
class="material-icons info-icon"
>info</i
>
</td>
</ng-container>
<ng-container matColumnDef="expandedDetail">
<td
mat-cell
*matCellDef="let element"
[attr.colspan]="displayedColumns.length"
class="locations-cell"
>
<div
class="example-element-detail"
[@detailExpand]="
element == expandedElement ? 'expanded' : 'collapsed'
"
>
<td-code-editor
[style.height.px]="200"
editorStyle="border:0;"
theme="vs"
[editorOptions]="editorOptions"
[(ngModel)]="element.messageBody"
>
</td-code-editor>
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr
mat-row
*matRowDef="let element; columns: displayedColumns"
class="example-element-row"
[class.example-expanded-row]="expandedElement === element"
(click)="expandedElement = expandedElement === element ? null : element"
></tr>
<tr
mat-row
class="example-detail-row"
*matRowDef="let row; columns: ['expandedDetail']"
></tr>
</table>
<app-paginator
[itemsPerPage]="20"
[numberOfEntries]="messages?.totalCount"
[currentPage]="page$ | async"
(pageChanged)="pageChange$.next($event)"
></app-paginator>
Part of the Typescript file:
expandedElement = null;
I hope somebody can help me here.
回答1:
I found my answer.
Apparentely I have to add <ng-template matTabContent></ng-template>
to the <mat-tab>
来源:https://stackoverflow.com/questions/64172189/angular-material-table-with-expandable-rows-automaticly-expanded-when-change-ta