简体   繁体   English

在列配置中设置 Angular Material mat-table 列宽

[英]Setting Angular Material mat-table column width in column configuration

I have the following column configuration for my Angular Material mat-table columns:我的 Angular Material mat-table列有以下列配置:

export interface TableColumn {
  name: string;
  dataKey: string;
  isSortable?: boolean;
  width?: string; // column width
}

//...

this.tableColumns = [
    {
        name: 'Id',
        dataKey: 'id',
        position: 'left',
        width: '50px'
    },
    {
        name: 'Name',
        dataKey: 'name',
        position: 'left',
        width: '100%'
    }
];

As far as I see, many people use css in order to set column width as on this page, etc. However, would not it be nice if we set the column widths for each column by pixel and percentage in the this.tableColumns array as the other properties when defining columns?据我所知,很多人使用 css 来设置页面上的列宽等。但是,如果我们在this.tableColumns数组中按像素和百分比设置每列的列宽,那不是很好定义列时的其他属性? I think I just need a configuration of mat-table , but I am not sure if there is such a config for mat-table .我想我只是需要的配置mat-table ,但我不知道是否有这样的配置mat-table If not, I think I can use [style.width] and set each column by column config value.如果没有,我想我可以使用 [style.width] 并按列配置值设置每一列。 Any suggestion for that?有什么建议吗?

In general, you defined the columns based in your array like:通常,您定义了基于数组的列,例如:

<ng-container *ngFor="let column of tableColumns;let first=first">
        <ng-container [matColumnDef]="column.dataKey">
            <th [style.width]="column.width" mat-header-cell *matHeaderCellDef>
                  {{column.name}}
            </th>
            <td [style.width]="column.width" mat-cell 
                 *matCellDef="let element"> {{element[column.dataKey]}}
            </td>
        </ng-container>
</ng-container>

And your displayedColumns as而你的displayColumns为

this.displayedColumns=this.tableColumns.map(x=>x.dataKey)

NOTE: I supose you defined the width as '50%' or '30px' if only use a number you can use, eg注意:如果只使用您可以使用的数字,我假设您将宽度定义为“50%”或“30px”,例如

[style.width]="column.width+'px'"

Update imagine you want to create a column with actions buttons, but this buttons can be one, two or three.更新假设您想创建一个带有操作按钮的列,但此按钮可以是一个、两个或三个。 We can asign to this last colum a width of "1%" and 'white-space: nowrap'我们可以为最后一列指定宽度为“1%”和“white-space: nowrap”

//we has two variables
btDelete:boolean=true;
btEdit:boolean:true;

<ng-container matColumnDef="action">
    <th style="width:1%" mat-header-cell *matHeaderCellDef>
    </th>
    <td style="width:1%;white-space: nowrap;" mat-cell 
         *matCellDef="let element">
        <button *ngIf="btEdit" mat-button (click)="edit(element)">Edit</button>
        <button *ngIf="btDelete" mat-button (click)="delete(element)">Delete</button> 
    </td>
</ng-container>

NOTE: I'm not prety sure what happens with the width of columns because the % total is bigger that 100%注意:我不太确定列的宽度会发生什么,因为 % total 大于 100%

We can try using the mat-table in flex-mode我们可以尝试在 flex-mode 下使用 mat-table

<ng-container *ngFor="let column of columns">
    <ng-container [matColumnDef]="column.name">
        <mat-header-cell [style.flex]="column.width" *matHeaderCellDef> {{column.width}}</mat-header-cell>
        <mat-cell [style.flex]="column.width" *matCellDef="let element"> {{element[column.name]}} </mat-cell>
    </ng-container>
</ng-container>
<ng-container matColumnDef="action">
    <mat-header-cell style="flex:0 1 auto;visibility:hidden" *matHeaderCellDef>
        <button *ngIf="btEdit" mat-button>Delete</button>
        <button *ngIf="btDelete" mat-button>Delete</button>
    </mat-header-cell>
    <mat-cell style="flex:0 1 auto" *matCellDef="let element"> <button *ngIf="btEdit" mat-button>Edit</button>
        <button *ngIf="btDelete" mat-button>Delete</button></mat-cell>
</ng-container>

See that the column "action" the "head" repeat the buttons -with visibility:hidden-看到列“动作”和“头”重复按钮 - 可见性:隐藏 -

NOTE: In this case, the "with" -really the flex- is a number, not a %,注意:在这种情况下,“with”-实际上是 flex- 是一个数字,而不是一个 %,

While there isn't a way to define column width in the array, you should be able to define it using ng-style.虽然没有办法在数组中定义列宽,但您应该能够使用 ng-style 来定义它。 It is possible to provide column specific properties in the html template, therefore this too should work.可以在 html 模板中提供特定于列的属性,因此这也应该有效。 An example would be一个例子是

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <mat-text-column name="position" [headerText]="headerText" [ngStyle]="{'width':'20%'}"></mat-text-column>

  <!-- Change the header text. -->
  <mat-text-column name="name" headerText="Element" [ngStyle]="{'width':'40%'}"></mat-text-column>

  <!-- Provide a data accessor for getting the cell text values. -->
  <mat-text-column name="weight" [dataAccessor]="getWeight" [ngStyle]="{'width':'40%'}"></mat-text-column>
</table>

The result of style.width can also be accomplished by ngStyle, so that too should work in the same way. style.width 的结果也可以由 ngStyle 完成,因此也应该以相同的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM