【发布时间】:2021-04-29 18:07:21
【问题描述】:
我有一个 Angular 9 mat-table,如下所示
<mat-table #table [dataSource]="dataSource">
<ng-container matColumnDef="category">
<mat-header-cell *matHeaderCellDef> category </mat-header-cell>
<mat-cell *matCellDef="let item">{{ item.category.path }}</mat-cell>
</ng-container>
<ng-container matColumnDef="title">
<mat-header-cell *matHeaderCellDef> item </mat-header-cell>
<mat-cell *matCellDef="let item"><a href='{{ item.path }}'>{{ item.title }}</a></mat-cell>
</ng-container>
<ng-container matColumnDef="price">
<mat-header-cell *matHeaderCellDef> price </mat-header-cell>
<mat-cell *matCellDef="let item">{{ item.created_on }}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
对于为每列设置固定宽度的各种列,我有以下样式
.mat-column-title {
word-wrap: break-word !important;
white-space: unset !important;
flex: 0 0 50% !important;
width: 50% !important;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
如何构建我的 CSS 以使该列只占用与数据占用一样多的宽度?也就是说,我不想为每一列指定一个固定的像素或百分比。不确定这是否可能。
【问题讨论】:
-
我不确定这在纯 CSS 中是否可行 :( 我会做什么如果我真的想创建一个具有动态列宽的表格,我想我会计算每列中的字符数,取每列的最大值,然后使用 'ch' 单位根据最大字符设置每列的宽度
-
如果解决方案不是纯基于 CSS 的,那也没关系。在普通的 HTML 中,我知道这绝对是可能的,我只是不知道如何构建我的 mat-table,所以它会生成那种 HTML。
-
进行封装:在你的 ts 文件中的 ViewEncapsulation.None @component.然后使用 css 它将按您的预期工作
-
这里是 stackblitz 示例:stackblitz.com/edit/angular-ivy-cfuxer?file=src/app/…
标签: css angular angular9 mat-table column-width