如何使用带条件的 mat table 扩展行

How to expand a row using mat table with a condition

我有一个包含不同列的 table,其中一列包含来自 Material 的 <mat-select><mat-option> html 标签,用于 Angular.

有不同的 serverdiskType.name.value,例如 'APP'、'LOG',...并且有一个特定的选项是 'CUSTOM'。

我希望在选择“自定义”选项时扩展该行,该怎么做?

我的代码:

我的模板:

 <table mat-table *ngIf=" resources.length>1" multiTemplateDataRows [dataSource]="dataSourceDisk" >

<!-- DiskType Column -->
        <ng-container matColumnDef="Type">
          <th mat-header-cell *matHeaderCellDef fxFlex="110px"> Disk Type </th>
          <td mat-cell (click)="$event.stopPropagation()" *matCellDef="let resources; let i=index; let element " fxFlex="110px">
            <mat-form-field>
              <mat-select placeholder="Storage Performance" [(ngModel)]="resources.resourceProperties.storageClass"
                [ngModelOptions]="{standalone: true}"
                (ngModelChange)="onChangeResourceProperties($event,'storageClass',i+1)">
                <mat-option *ngFor="let serverdiskType of serverdiskTypes" [value]="serverdiskType.name.value" 
                (click)="EXPAND THE ROW WHEN serverdiskType.name.value EQUALS TO 'CUSTOM'">
                  {{ serverdiskType.name.viewValue }}
                </mat-option>
              </mat-select>
            </mat-form-field>
          </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 disk" [attr.colspan]="displayedDiskColumns.length">
            <div class="example-element-detail" [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
              <div class="example-container">
                <mat-form-field>
                  <input matInput [(ngModel)]="disk.resourceProperties.ioPerMonth" name="ioPerMonth" placeholder="IO/month">
                </mat-form-field>

                <mat-form-field>
                  <input matInput [(ngModel)]="disk.resourceProperties.iops" name="iops" placeholder="IO/S">
                </mat-form-field>
              </div>
            </div>
          </td>
        </ng-container>

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

</table>

您需要像下面这样处理扩展。表达式将是

[@detailExpand]="element.diskType == 'custom' ? 'expanded' : 'collapsed'">

<ng-container matColumnDef="expandedDetail">
    <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
      <div class="example-element-detail"
           [@detailExpand]="element.diskType == 'custom' ? 'expanded' : 'collapsed'">
        <div class="example-container">
                <mat-form-field>
                  <input matInput [(ngModel)]="element.ioPerMonth" name="element.ioPerMonth" placeholder="IO/month">
                </mat-form-field>

                <mat-form-field>
                  <input matInput [(ngModel)]="element.iops" name="element.iops" placeholder="IO/S">
                </mat-form-field>
              </div>
      </div>
    </td>
  </ng-container>

工作演示在这里.. https://stackblitz.com/edit/angular-94fmby 希望这有帮助。