3

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 enter image description here

After I switched tabs and return to the Messages tab enter image description here

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.

Koen Rombout
  • 220
  • 2
  • 4
  • 15

1 Answers1

3

I found my answer. Apparentely I have to add <ng-template matTabContent></ng-template> to the <mat-tab>

Koen Rombout
  • 220
  • 2
  • 4
  • 15
  • I am facing the same issue when I re route to my page which has the table. Could you find why your fix worked? – humbleCoder Dec 29 '20 at 18:27
  • Apparently it is documented in the angular docs somewhere. I looked over it. – Koen Rombout Dec 30 '20 at 19:49
  • In this link you it is documented that a mat-tab content is requierd in a mat-tab and I forgot that. https://material.angular.io/components/tabs/overview – Koen Rombout Dec 30 '20 at 19:52
  • Ya but I am not using tabs and still get the issue. Not sure why. – humbleCoder Dec 31 '20 at 05:56
  • Can you send a codepen? maybe we can look together? – Koen Rombout Jan 04 '21 at 10:18
  • 1
    I found the problem. Check this answer for details : https://stackoverflow.com/a/65540796/1711670. The sorting breaks the animation incase the datasource is custom. Used simple css instead of angular animation to fulfill my requirements. – humbleCoder Jan 04 '21 at 10:47