デフォルトでは、Angular Material Table は、コンテンツがセルからあふれた場合、そのコンテンツを新しい行に表示します。私は、あふれたテキストが「...」で切り捨てられるように設定しようとしています。現在使用しているコードでは、コンテンツは切り捨てられず、親からあふれ出るまで 1 行で表示されます。<div>
<table mat-table>
の代わりにを使用するのは<mat-table>
、 で説明されているように、コンテンツに基づいてサイズが調整される列があるためです。Angular マテリアル ウェブサイト私が作成しようとしているテーブルはレスポンシブで、親のサイズに基づいてサイズが変更されます<div>
HTML:
<div class="table-content">
<table mat-table [dataSource]="dataSource" class="table-container">
<ng-container [matColumnDef]="item" *ngFor="let item of displayedColumns">
<th mat-header-cell *matHeaderCellDef>{{item}} </th>
<td mat-cell *matCellDef="let element">
<div class="table-data-content">
<span>{{element[item]}}</span>
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns;"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
CS: ...
.table-container {
position: absolute;
width: 100%;
height: 100%;
}
.table-data-content {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
Angular コンポーネントで使用されるサンプルデータ:
dataSource = [
{ Name: "Some file name that is very long", Date: '08/30/2017', Uploader: 'Some uploader name that is very long'},
{ Name: "Some file name that is very long", Date: '08/30/2017', Uploader: 'Some uploader name that is very long'}
];
displayedColumns = ['Name', 'Date', 'Uploader'];
表内で省略記号を適切に使用するには何を修正すればよいでしょうか?
アップデート:次の CSS を追加すると省略記号が機能しますが、その理由はまだわかりません。
td {
max-width: 0px;
}
ただし、このアプローチでは、Angular Material Table がコンテンツの長さに基づいて各列の幅を効率的に割り当てる方法が妨げられ、他の列にまだ多くの空きスペースが残っている場合にコンテンツが切り捨てられます。これに対するより良い解決策はありますか?
ベストアンサー1
一番の問題は、幅と高さを「table-content」に適用していることです。代わりに、直接適用する必要があります。テーブルタグ。
これを試して:
table {
width: 100%;
table-layout: fixed;
}
th, td {
overflow: hidden;
width:auto;
text-overflow: ellipsis;
white-space: nowrap;
}
の上テーブルタグの使用テーブルレイアウト: fixed が適用されると、コンテンツによってレイアウトが決定されなくなり、代わりにブラウザーはテーブルの最初の行に定義された幅を使用して列の幅を定義します。