Mat-Table内的Mat-Table显示每个记录的额外行收件箱x

时间:2018-10-15 10:08:28

标签: angular material

我已经在Mat-Table中实现了Mat-Table。显示的数据正确。但是,对于父行的每一行,它在Mat-Table中显示了一个额外的空行。由于我无法找到错误,因此我在此论坛上寻求帮助。请帮忙。

HTML代码如下。

  <div class="mat-elevation-z8">
    <table mat-table [dataSource]="listData" class="mat-elevation-z8" matSort multiTemplateDataRows width="100%">

      <!--- Note that these columns can be defined in any order.
              The actual rendered columns are set as a property on the row definition" -->

      <!-- Tax Code Column -->
      <ng-container matColumnDef="taxCode">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Tax Code </th>
        <td mat-cell *matCellDef="let element" align="left"> {{element.taxCode}} </td>
      </ng-container>

      <!-- Tax Name Column -->
      <ng-container matColumnDef="taxDescription">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
        <td mat-cell *matCellDef="let element" align="left"> {{element.taxDescription}} </td>
      </ng-container>

      <!-- Tax Type Code Column -->
      <ng-container matColumnDef="taxTypeCode">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Tax Type Code </th>
        <td mat-cell *matCellDef="let element" align="left"> {{element.taxTypeCode}} </td>
      </ng-container>

      <!-- Edit Column -->
      <ng-container matColumnDef="edit">
        <th mat-header-cell *matHeaderCellDef>
        </th>
        <td mat-cell *matCellDef="let row">
            <a mat-button matTooltip="Edit" [routerLink]="['/tax/edit', row.taxCode]" *ngIf="isUserAuthenticated"><mat-icon>create</mat-icon></a>
        </td>
      </ng-container>

      <!-- Delete Column -->
      <ng-container matColumnDef="delete">
        <th mat-header-cell *matHeaderCellDef>
        </th>
        <td mat-cell *matCellDef="let row">
            <button mat-button matTooltip="Delete" color="warn" (click)="onDelete(row.taxCode)" *ngIf="isUserAuthenticated"><mat-icon>clear</mat-icon></button>
        </td>
      </ng-container>

      <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
      <ng-container matColumnDef="expandedDetail">
        <td mat-cell *matCellDef="let element" [attr.colspan]="displayedColumns.length">
          <div class="example-element-detail"
              [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'" *ngIf="element.taxVersion.length > 0">

            <table mat-table [dataSource]="element.taxVersion" class="mat-elevation-z8" width="100%">

              <ng-container matColumnDef="taxID">
                <th mat-header-cell *matHeaderCellDef> Tax ID </th>
                <td mat-cell *matCellDef="let elem"> {{elem.taxID}} </td>
              </ng-container>
            
              <ng-container matColumnDef="versionCode">
                <th mat-header-cell *matHeaderCellDef> Version Code </th>
                <td mat-cell *matCellDef="let elem"> {{elem.versionCode}} </td>
              </ng-container>
            
              <ng-container matColumnDef="versionDate">
                <th mat-header-cell *matHeaderCellDef> Version Date </th>
                <td mat-cell *matCellDef="let elem"> {{elem.versionDate}} </td>
              </ng-container>
            
              <ng-container matColumnDef="taxRate">
                <th mat-header-cell *matHeaderCellDef> Tax Rate </th>
                <td mat-cell *matCellDef="let elem"> {{elem.taxRate}} </td>
              </ng-container>
            
              <tr mat-header-row *matHeaderRowDef="displayedColumnsVersion"></tr>
              <tr mat-row *matRowDef="let row; columns: displayedColumnsVersion;"></tr>
            </table>
          </div>
        </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"
                          class="example-element-row"
                          [class.example-expanded-row]="expandedElement === row"
                          (click)="expandedElement = row"
      >
      </tr>
      <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
    </table>

    <mat-paginator [pageSizeOptions]="[7, 15, 20]" showFirstLastButtons></mat-paginator>
  </div>

enter image description here

2 个答案:

答案 0 :(得分:0)

如果没有element.taxVersion,则需要隐藏ng-container,现在看来,如果长度大于0,则只显示ng-container的内部,留下一个空容器。

<ng-container *ngIf="element.taxVersion.length > 0" matColumnDef="expandedDetail">

答案 1 :(得分:0)

不幸的是,在另一个垫子桌子内使用垫子桌子会产生副作用。 Mat-table将在其范围内渲染所有mat-row,包括内部表中的。您可以为您的<tr mat-row *matRowDef="let row; columns: displayedColumnsVersion;"></tr>行添加一个额外的类class='inner-table-row'并检查该额外的行以查看是否是问题所在。解决此问题的最佳选择是将内部表移动到另一个组件中,并使用选择器将其呈现在外部表中。