Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First column fixed in mat-table

do you happen to know how to fix the first column and use horizontal scrolling in mat-table in Angular Material?

I would be very grateful for your help.

like image 538
and.neo2020 Avatar asked Aug 01 '18 18:08

and.neo2020


2 Answers

Add a method to you component, let's call it isSticky(column). Then bind [sticky] to it. See below for the entire working example link.

HTML

<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>

TS

isSticky (column: string): boolean {
  return column === 'col1' ? true : false;
}

Full Example

StackBlitz - angular-sticky-table

like image 26
davepmiller Avatar answered Oct 03 '22 03:10

davepmiller


You should use mat-table API for this - sticky value.

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

Please check in official doc: https://material.angular.io/components/table/examples example name: "Table with a sticky columns"

like image 142
profiler Avatar answered Oct 03 '22 01:10

profiler