Ano*_*ick 3 css typescript angular-material angular angular-material-table
我正在尝试制作一个 Angular 组件来显示数据和接收数据。我制作了一个带有输入类型表单字段的 mat-table 和带有 {{element.value}} 的常规数据显示。我将每列的宽度设置为不同的,如下所示:
.mat-column-v{
width: 32%!important;
}
.mat-column-w{
width: 17%!important;
}
.mat-column-x{
width: 17%!important;
}
.mat-column-y{
width: 17%!important;
}
.mat-column-z{
width: 17%!important;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我使用输入模式时,列的宽度设置不正确。另一方面,输出模式可以正常工作,如下图所示:

我必须覆盖这些信息,但右侧是输入模式,第一列具有某种随机宽度,左侧是宽度设置正确的输出模式。
我的组件的代码:
<table mat-table [dataSource]="data">
<!-- v Column -->
<ng-container matColumnDef="v">
<th mat-header-cell *matHeaderCellDef> v </th>
<td mat-cell *matCellDef="let element"> {{element.v}} </td>
</ng-container>
<!-- w Column -->
<ng-container matColumnDef="w">
<th mat-header-cell *matHeaderCellDef> w </th>
<td mat-cell *matCellDef="let element">
<ng-container *ngIf="type == 'input'">
<mat-form-field>
<input matInput [value]="element.w" [(ngModel)]="element.w">
</mat-form-field>
</ng-container>
<ng-container *ngIf="type == 'output'"> {{element.w}} </ng-container>
</td>
</ng-container>
<!-- x Column -->
<ng-container matColumnDef="x">
<th mat-header-cell *matHeaderCellDef> x </th>
<td mat-cell *matCellDef="let element">
<ng-container *ngIf="type == 'input'">
<mat-form-field>
<input matInput [value]="element.x" [(ngModel)]="element.x">
</mat-form-field>
</ng-container>
<ng-container *ngIf="type == 'output'"> {{element.x}} </ng-container>
</td>
</ng-container>
<!-- y Column -->
<ng-container matColumnDef="y">
<th mat-header-cell *matHeaderCellDef> y </th>
<td mat-cell *matCellDef="let element">
<ng-container *ngIf="type == 'input'">
<mat-form-field>
<input matInput [value]="element.y" [(ngModel)]="element.y">
</mat-form-field>
</ng-container>
<ng-container *ngIf="type == 'output'"> {{element.y}} </ng-container>
</td>
</ng-container>
<!-- z Column -->
<ng-container matColumnDef="z">
<th mat-header-cell *matHeaderCellDef> z </th>
<td mat-cell *matCellDef="let element">
<ng-container *ngIf="type == 'input'">
<mat-form-field>
<input matInput [value]="element.z" [(ngModel)]="element.z">
</mat-form-field>
</ng-container>
<ng-container *ngIf="type == 'output'"> {{element.z}} </ng-container>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="heads"></tr>
<tr mat-row *matRowDef="let row; columns: heads;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我的组件的 CSS:
table {
width: 100%;
}
.mat-form-field {
font-size: 14px;
width: 90%;
}
.mat-column-v{
width: 32%!important;
}
.mat-column-w{
width: 17%!important;
}
.mat-column-x{
width: 17%!important;
}
.mat-column-y{
width: 17%!important;
}
.mat-column-z{
width: 17%!important;
}
Run Code Online (Sandbox Code Playgroud)
如何设置包含输入的表中的第一列大小,就像其他列一样?
您可以在此处查看完整的演示
我在顶部添加了一个切换按钮,以便您可以检查您想要达到的确切影响......
我们必须覆盖设置为 180px 的输入的默认宽度...我们使用下面的 css 来做到这一点:
::ng-deep .mat-column-w div.mat-form-field-infix,
::ng-deep .mat-column-x div.mat-form-field-infix,
::ng-deep .mat-column-y div.mat-form-field-infix,
::ng-deep .mat-column-z div.mat-form-field-infix { width: 17% !important; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4847 次 |
| 最近记录: |