固定在 mat-table 中的第一列

and*_*020 8 datatable angular-material angular

您是否知道如何修复第一列并在 Angular Material 的 mat-table 中使用水平滚动?

我将非常感谢您的帮助。

pro*_*ler 11

您应该mat-table为此使用API -sticky值。

<ng-container matColumnDef="name" sticky>
  <th mat-header-cell *matHeaderCellDef> Name </th>
  <td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

请查看官方文档:https : //material.angular.io/components/table/examples 示例名称:“带有粘性列的表格”


dav*_*ler 2

向您的组件添加一个方法,我们称之为isSticky(column)。然后绑定[sticky]到它。请参阅下面的完整工作示例链接。

超文本标记语言

<div class="app-table-wrapper">
  <table mat-table [dataSource]="dataSource" id="app-table">
      <ng-container *ngFor="let column of displayedColumns" matColumnDef={{column}} [sticky]="isSticky(column)">
          <th mat-header-cell *matHeaderCellDef> {{column}} </th>
          <td mat-cell *matCellDef="let element">{{element[column]}}</td>
      </ng-container>
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>S
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
</div>
Run Code Online (Sandbox Code Playgroud)

TS

isSticky (column: string): boolean {
  return column === 'col1' ? true : false;
}
Run Code Online (Sandbox Code Playgroud)

完整示例

StackBlitz - 角度粘性表

  • 谢谢,这正是我需要的解决方案。几个月后,我在开发过程中使用了这个功能,我很满意。 (2认同)