如何在角度设置垫子桌子的宽度?

DDD*_*DDD 22 html css angular-material angular

在我的mat-table中,这里有6列,当任何一列没有更多的单词时,它看起来像Image-1,但是当任何一列有更多单词时,UI看起来像Image-2,那么如何将UI设置成像Image-1一样列中的第6个角中有更多单词?

图片1

在此处输入图片说明

图片2

在此处输入图片说明

user.component.html

<div class="mat-elevation-z8">      
 <table mat-table [dataSource]="dataSource">
  <ng-container matColumnDef="userimage">
    <th mat-header-cell *matHeaderCellDef> # </th>
    <td mat-cell *matCellDef="let element"> 
      <img src="{{commonUrlObj.commonUrl}}/images/{{element.userimage}}" style="height: 40px;width: 40px;"/>
    </td>
  </ng-container>

  <ng-container matColumnDef="username">
    <th mat-header-cell *matHeaderCellDef> Full Name </th>
    <td mat-cell *matCellDef="let element"> {{element.username}} ( {{element.usertype}} )</td>
  </ng-container>

  <ng-container matColumnDef="emailid">
    <th mat-header-cell *matHeaderCellDef> EmailId </th>
    <td mat-cell *matCellDef="let element"> {{element.emailid}} </td>
   </ng-container>

  <ng-container matColumnDef="contactno">
    <th mat-header-cell *matHeaderCellDef> Contact No. </th>
    <td mat-cell *matCellDef="let element"> {{element.contactno}} </td>
  </ng-container>

  <ng-container matColumnDef="enabled">
    <th mat-header-cell *matHeaderCellDef> Enabled </th>
    <td mat-cell *matCellDef="let element" style="color: blue">
      <ng-container *ngIf="element.enabled == 'true'; else otherss">Enabled</ng-container>
        <ng-template #otherss>Disabled</ng-template>
    </td>
  </ng-container>

  <ng-container matColumnDef="action">
    <th mat-header-cell *matHeaderCellDef> Action </th>
      <td mat-cell *matCellDef="let element" fxLayoutGap="5px">
        <button mat-mini-fab color="primary" routerLink="/base/editUserDetails/{{element.userid}}"><mat-icon>edit</mat-icon></button>
        <button mat-mini-fab color="primary" routerLink="/base/viewUserDetails/{{element.userid}}"><mat-icon>pageview</mat-icon></button>
      </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20, 50 ,100]" showFirstLastButtons></mat-paginator>
Run Code Online (Sandbox Code Playgroud)

San*_*tel 67

正如我所实施的,它运行良好。您只需要使用添加列宽matColumnDef="description"

例如 :

<mat-table #table [dataSource]="dataSource" matSortDisableClear>
    <ng-container matColumnDef="productId">
        <mat-header-cell *matHeaderCellDef>product ID</mat-header-cell>
        <mat-cell *matCellDef="let product">{{product.id}}</mat-cell>
    </ng-container>
    <ng-container matColumnDef="productName">
        <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
        <mat-cell *matCellDef="let product">{{product.name}}</mat-cell>
    </ng-container>
    <ng-container matColumnDef="actions">
        <mat-header-cell *matHeaderCellDef>Actions</mat-header-cell>
        <mat-cell *matCellDef="let product">
            <button (click)="view(product)">
                <mat-icon>visibility</mat-icon>
            </button>
        </mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)

matColumnDefproductIdproductName并且action

现在我们应用宽度 matColumnDef

造型

.mat-column-productId {
    flex: 0 0 10%;
}
.mat-column-productName {
    flex: 0 0 50%;
}
Run Code Online (Sandbox Code Playgroud)

剩余宽度平均分配给其他列

  • 我认为这是最好的答案。这确实对我有帮助。寻找解决方案有一段时间了。谢谢你! (4认同)

小智 44

你可以很容易地做到这一点。在每一列中,您将获得一个字段名称以 mat-column 为前缀的类,因此该类将类似于 mat-column-yourFieldName。因此,您可以设置如下样式

.mat-column-yourFieldName {
    flex: none;
    width: 100px;
}
Run Code Online (Sandbox Code Playgroud)

所以我们可以根据我们的要求为列提供固定宽度。

希望这对某人有帮助。


DDD*_*DDD 34

使用css我们可以调整我在下面的代码中放入的特定列宽。

user.component.css

table{
 width: 100%;
}

.mat-column-username {
  word-wrap: break-word !important;
  white-space: unset !important;
  flex: 0 0 28% !important;
  width: 28% !important;
  overflow-wrap: break-word;
  word-wrap: break-word;

  word-break: break-word;

  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}

.mat-column-emailid {
  word-wrap: break-word !important;
  white-space: unset !important;
  flex: 0 0 25% !important;
  width: 25% !important;
  overflow-wrap: break-word;
  word-wrap: break-word;

  word-break: break-word;

  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}

.mat-column-contactno {
  word-wrap: break-word !important;
  white-space: unset !important;
  flex: 0 0 17% !important;
  width: 17% !important;
  overflow-wrap: break-word;
  word-wrap: break-word;

  word-break: break-word;

  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}

.mat-column-userimage {
  word-wrap: break-word !important;
  white-space: unset !important;
  flex: 0 0 8% !important;
  width: 8% !important;
  overflow-wrap: break-word;
  word-wrap: break-word;

  word-break: break-word;

  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}

.mat-column-userActivity {
  word-wrap: break-word !important;
  white-space: unset !important;
  flex: 0 0 10% !important;
  width: 10% !important;
  overflow-wrap: break-word;
  word-wrap: break-word;

  word-break: break-word;

  -ms-hyphens: auto;
  -moz-hyphens: auto;
  -webkit-hyphens: auto;
  hyphens: auto;
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该添加将这些类添加到的选择器 (8认同)
  • 为什么是`!important`?尽可能避免使用该指令,因为没有该指令也可以正常工作。 (6认同)
  • @Nick:您不会添加到数据表的任何选择器。只需将您的 css 选择器命名为与您在 .html 文件中为 matColumnDef 指定的名称相同即可 (3认同)

Sim*_*ver 23

如果您使用scss作为样式,则可以使用mixin来帮助生成代码。如果您每次都放置所有属性,则样式将很快失去控制。

这是一个非常简单的示例-实际上只是概念证明,您可以根据需要使用多个属性和规则进行扩展。

@mixin mat-table-columns($columns)
{
    .mat-column-
    {
        @each $colName, $props in $columns {

            $width: map-get($props, 'width');

            &#{$colName} 
            {
                flex: $width;
                min-width: $width;

                @if map-has-key($props, 'color') 
                {
                    color: map-get($props, 'color');
                }
            }  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在定义表的组件中执行以下操作:

@include mat-table-columns((

    orderid: (width: 6rem, color: gray),
    date: (width: 9rem),
    items: (width: 20rem)

));
Run Code Online (Sandbox Code Playgroud)

这将生成如下内容:

.mat-column-orderid[_ngcontent-c15] {
  flex: 6rem;
  min-width: 6rem;
  color: gray; }

.mat-column-date[_ngcontent-c15] {
  flex: 9rem;
  min-width: 9rem; }
Run Code Online (Sandbox Code Playgroud)

在此版本中width成为flex: value; min-width: value

对于您的特定示例,您可以添加wrap: true或类似的东西作为新参数。

  • 注意:如果您愿意,当然可以使用像素。当涉及文本数据时,我通常更喜欢 ems,并且我在这里使用 rems,因为当标题和单元格字体大小不同时,它们会倾斜,因为根据定义,“em”随字体大小而变化。 (2认同)
  • 这看起来非常漂亮,但我不清楚将 mixin 放在哪里...... (2认同)

Pra*_*ale 17

您可以使用以下CSS来做到这一点:

table {
  width: 100%;
  table-layout: fixed;
}

th, td {
  overflow: hidden;
  width: 200px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)

这是StackBlitz Example带有样本数据的

  • 这是关于“mat-table”而不是“table[mat-table]”。这不适用于“mat-table”(flex)。https://material.angular.io/components/table/overview (9认同)

小智 11

这是解决问题的另一种方法:

为什么不在表需要尝试将其放入其列之前截断描述,而不是尝试“在后期修复它”?我是这样做的:

<ng-container matColumnDef="description">
   <th mat-header-cell *matHeaderCellDef> {{ 'Parts.description' | translate }} </th>
            <td mat-cell *matCellDef="let element">
                {{(element.description.length > 80) ? ((element.description).slice(0, 80) + '...') : element.description}}
   </td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

所以我首先检查数组是否大于某个长度,如果是,则截断并添加 '...' 否则按原样传递值。这使我们仍然可以从表格的自动间距中受益:)

在此处输入图片说明


小智 6

我们可以 直接给 th添加属性宽度

例如:

<ng-container matColumnDef="position" >
    <th mat-header-cell *matHeaderCellDef width ="20%"> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>
Run Code Online (Sandbox Code Playgroud)

  • 截至目前,TH 标签上不推荐使用宽度。 (8认同)

Yas*_*nto 6

只需要更新标签的宽度即可th

th {
  width: 100px;
}
Run Code Online (Sandbox Code Playgroud)