Angular Material表格水平对齐单元格元素

huangapple go评论56阅读模式
英文:

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: flexdisplay: inline-flex,这确实可以使图标水平对齐,但会破坏单元格的显示,使元素变形。

我相信有一种简单的方法可以解决这个问题。有什么想法吗?

提前感谢。

英文:

I have an angular material table with a column who's cells contain multiple google material icons like so:

&lt;table mat-table [dataSource]=&quot;artistRateDataSource&quot; class=&quot;mat-elevation-z8&quot;&gt;

  &lt;ng-container matColumnDef=&quot;action&quot;&gt;

    &lt;th mat-header-cell *matHeaderCellDef&gt; Action &lt;/th&gt;

    &lt;td mat-cell *matCellDef=&quot;let rate&quot; class=&quot;rate-action-container&quot;&gt;
      &lt;i matTooltip=&quot;Edit rate&quot; class=&quot;material-icons icon-button&quot;&gt;edit&lt;/i&gt;
      &lt;i matTooltip=&quot;Delete rate&quot; class=&quot;material-icons icon-button&quot;&gt;delete_forever&lt;/i&gt;
    &lt;/td&gt;

  &lt;/ng-container&gt;

&lt;/table&gt;

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:

&lt;table mat-table [dataSource]=&quot;artistRateDataSource&quot; class=&quot;mat-elevation-z8&quot;&gt;
  &lt;ng-container matColumnDef=&quot;action&quot;&gt;
    &lt;th mat-header-cell *matHeaderCellDef&gt;Action&lt;/th&gt;
    &lt;td mat-cell *matCellDef=&quot;let rate&quot; class=&quot;rate-action-container&quot;&gt;
      &lt;div class=&quot;action-button-container&quot;&gt;
        &lt;i matTooltip=&quot;Edit rate&quot; class=&quot;material-icons icon-button&quot;&gt;edit&lt;/i&gt;
        &lt;i matTooltip=&quot;Delete rate&quot; class=&quot;material-icons icon-button&quot;&gt;delete_forever&lt;/i&gt;
      &lt;/div&gt;
    &lt;/td&gt;
  &lt;/ng-container&gt;
&lt;/table&gt;

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!

huangapple
  • 本文由 发表于 2023年4月10日 22:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定