英文:
Angular Material Table align cell elements horizontally
问题
我有一个使用Angular Material的表格,其中一列的单元格包含多个Google Material图标,如下所示:
<table mat-table [dataSource]="artistRateDataSource" class="mat-elevation-z8">
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef> Action </th>
<td mat-cell *matCellDef="let rate" class="rate-action-container">
<i matTooltip="Edit rate" class="material-icons icon-button">edit</i>
<i matTooltip="Delete rate" class="material-icons icon-button">delete_forever</i>
</td>
</ng-container>
</table>
我最近升级到了最新版本的Angular Material(v15.2.6)。
在更新之前,图标在单元格内水平显示,但在更新后,它们现在垂直堆叠显示。
我尝试在相关单元格中使用 display: flex
和 display: inline-flex
,这确实可以使图标水平对齐,但会破坏单元格的显示,使元素变形。
我相信有一种简单的方法可以解决这个问题。有什么想法吗?
提前感谢。
英文:
I have an angular material table with a column who's cells contain multiple google material icons like so:
<table mat-table [dataSource]="artistRateDataSource" class="mat-elevation-z8">
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef> Action </th>
<td mat-cell *matCellDef="let rate" class="rate-action-container">
<i matTooltip="Edit rate" class="material-icons icon-button">edit</i>
<i matTooltip="Delete rate" class="material-icons icon-button">delete_forever</i>
</td>
</ng-container>
</table>
I recently updated to the most recent version of Angular Material (v15.2.6).
Before updating the icons were displayed horizontally within the cell, but since updating they now stack vertically.
I have attempted using display: flex
and display: inline-flex
for the cell in question. This did align the icons horizontally, but messed up the display of the cell, squashing the elements.
I'm sure there is a simple way to fix, this. Any ideas?
Thanks in advance.
答案1
得分: 1
你可以尝试将你的按钮放在一个带有 display: flex;
样式的容器内,就像这样:
CSS:
.action-button-container {
display: flex;
width: 100px;
text-align: center;
}
此外,你可能需要对你的 rate-action-container
类进行一些样式调整,以使其具有固定的宽度,类似于 width: max-content
。
我没有加载这个解决方案到你的示例代码中,但我使用了类似的方法。另外,免责声明,我是在 Angular 14 上操作的。如果这有效,请告诉我,否则我可以尝试为你提供其他解决方案。
祝好运!
英文:
You could try adding a container around your buttons with display: flex;
like so:
<table mat-table [dataSource]="artistRateDataSource" class="mat-elevation-z8">
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef>Action</th>
<td mat-cell *matCellDef="let rate" class="rate-action-container">
<div class="action-button-container">
<i matTooltip="Edit rate" class="material-icons icon-button">edit</i>
<i matTooltip="Delete rate" class="material-icons icon-button">delete_forever</i>
</div>
</td>
</ng-container>
</table>
CSS:
.action-button-container {
display: flex;
width: 100px;
text-align: center;
}
Also, you may need to add some style adjustments to your rate-action-container
class to give it a static width as well. Something like width: max-content
.
I did not load this solution with your sample code, but have used something similar. Also, disclaimer, I am on Angular 14. Let me know if this works, other wise I can try to come up with something else for you.
Cheers!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论