##はじめに
Angular MaterialのTableで表を作成していたのですが、ヘッダー固定をするためにCSSですごい色々試した結果、Material内にhtmlの変更だけで済むすごく簡単な方法が用意されていましたという話。
Angularでヘッダー固定しようとしている人が同じ轍を踏まないために。
Angular Material公式ドキュメントを参考にしました。
Angular Material:https://material.angular.io/
##環境
OS: MacOS
Angular CLI: 10.1.0
Node: 10.16.0
ブラウザ: Chrome(Edgeでも動作確認済み)
##実装方法
コードは公式Angular Material内にあるテンプレートを利用します。
https://stackblitz.com/angular/arajpempodg?file=src%2Fapp%2Ftable-basic-example.html
実装は簡単です。
テーブルを記述しているhtmlのtrタグ内に sticky: true
を追加するだけです。
実際のコードは以下になります。
<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 mat-header-cell *matHeaderCellDef> 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 mat-header-cell *matHeaderCellDef> Symbol </th>
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<!-- Copyright 2020 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license -->
ヘッダが固定されているかの確認tsは以下になります。(スクロールをさせるためだけに行の追加を行なっただけです)
import { Component } from "@angular/core";
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
{ position: 1, name: "Hydrogen", weight: 1.0079, symbol: "H" },
{ position: 2, name: "Helium", weight: 4.0026, symbol: "He" },
{ position: 3, name: "Lithium", weight: 6.941, symbol: "Li" },
{ position: 4, name: "Beryllium", weight: 9.0122, symbol: "Be" },
{ position: 5, name: "Boron", weight: 10.811, symbol: "B" },
{ position: 6, name: "Carbon", weight: 12.0107, symbol: "C" },
{ position: 7, name: "Nitrogen", weight: 14.0067, symbol: "N" },
{ position: 8, name: "Oxygen", weight: 15.9994, symbol: "O" },
{ position: 9, name: "Fluorine", weight: 18.9984, symbol: "F" },
{ position: 10, name: "Neon", weight: 20.1797, symbol: "Ne" },
{ position: 11, name: "Hydrogen", weight: 1.0079, symbol: "H" },
{ position: 12, name: "Helium", weight: 4.0026, symbol: "He" },
{ position: 13, name: "Lithium", weight: 6.941, symbol: "Li" },
{ position: 14, name: "Beryllium", weight: 9.0122, symbol: "Be" },
{ position: 15, name: "Boron", weight: 10.811, symbol: "B" },
{ position: 16, name: "Carbon", weight: 12.0107, symbol: "C" },
{ position: 17, name: "Nitrogen", weight: 14.0067, symbol: "N" },
{ position: 18, name: "Oxygen", weight: 15.9994, symbol: "O" },
{ position: 19, name: "Fluorine", weight: 18.9984, symbol: "F" },
{ position: 20, name: "Neon", weight: 20.1797, symbol: "Ne" },
];
/**
* @title Basic use of `<table mat-table>`
*/
@Component({
selector: "table-basic-example",
styleUrls: ["table-basic-example.css"],
templateUrl: "table-basic-example.html"
})
export class TableBasicExample {
displayedColumns: string[] = ["position", "name", "weight", "symbol"];
dataSource = ELEMENT_DATA;
}
/** Copyright 2020 Google LLC. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
以上でヘッダーのスクロールの実装は終わりです。
ソースの書き換えを行うとヘッダーが固定されてスクロールが可能となるはずです。
##注意
- テーブルの構成が
<th>...</th>
<td>...</td>
になっていないと機能しない点に注意です。th、tdタグを利用していないテーブルではヘッダーは固定されません。 -
sticky: true
の挿入場所に注意してください。matHeaderRowDefの二重クオート内に入れないとコンパイルエラーします。
##おわりに
htmlの書き換えだけでヘッダーの固定ができるとはAngularはすごいと思いました、が、もうちょっと早く気付けたら良かった...。
詳細は公式ドキュメントを参照ください。( Sticky Rows and Columns 部分に記載があります。)
https://material.angular.io/components/table/overview