如何拆分角度材料表中的标题

Cat*_*nec 5 html-table typescript angular-material angular

如何在 Angular Material 中制作父级和子级等标题?

我想要这样的东西: 姓名&(名字、姓氏)

我只找到了这个例子: https://stackblitz.com/edit/angular-bklajw ?file=app%2Ftable-basic-example.html

但我不需要 2 行标题。

Awa*_*ais 5

欢迎来到 SO,添加了 3 个标头组,其中colnrow span在上面的 stack-blitz 代码中使用下面的 HTMl 来查看结果

 <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

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

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th [ngStyle]="{'display': 'none'}" mat-header-cell *matHeaderCellDef [attr.rowspan]="2">  No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

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

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th [attr.rowspan]="2" mat-header-cell *matHeaderCellDef [ngStyle]="{'display': 'none'}"> Symbol </th>
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

  <!-- Header row first group -->
  <ng-container matColumnDef="header-row-first-group">
    <th mat-header-cell *matHeaderCellDef 
        [style.text-align]="center"
        [attr.rowspan]="2"> 
      No
    </th>
     </ng-container>
    <ng-container matColumnDef="header-row-sec-group">
     <th mat-header-cell *matHeaderCellDef 
        [style.text-align]="center"
        [attr.colspan]="2"> 
      Name 
    </th>
     </ng-container>
    <ng-container matColumnDef="header-row-third-group">
     <th mat-header-cell *matHeaderCellDef 
        [style.text-align]="center"
       [attr.rowspan]="2" > 
      Role
    </th>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="['header-row-first-group','header-row-sec-group','header-row-third-group']"></tr>
  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

  • @Catalonec 更新了我的答案,请看一下,让我看看它是否有效 (2认同)
  • @app你能粗略地解释一下请喜欢一个简单的草图吗 (2认同)
  • /sf/ask/4570756291/ (2认同)