【发布时间】:2019-12-22 17:57:30
【问题描述】:
在我的项目中,我使用 Angular Material 的表格以表格格式显示值,根据新要求,如果用户单击另一列,我必须对最后 2 列执行在线编辑列会自动突出显示,一切都必须使用 Angular 材料完成
这是我要执行内联编辑的最后 2 列
> <ng-container matColumnDef="weight">
> <th mat-header-cell *matHeaderCellDef> Weight </th>
> <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
> </ng-container>
> <ng-container matColumnDef="symbol">
> <th mat-header-cell *matHeaderCellDef> Symbol </th>
> <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
> </ng-container>
>
>
> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table>
我的实现方式如下:
<tbody>
<tr *ngFor="let data of dataSource">
<td >{{data.position}}</td>
<td >{{data.name}}</td>
<td *ngIf="data.position === editRowId"> <input matInput [(ngModel)]="data.weight"></td>
<td *ngIf="data.position !== editRowId" (click)="editTableRow(data.position)">{{data.weight}}</td>
<td *ngIf="data.position === editRowId"> <input matInput [(ngModel)]="data.symbol"></td>
<td *ngIf="data.position !== editRowId" (click)="editTableRow(data.position)">{{data.symbol}}</td>
</tr>
</tbody>
上述代码的 TS 文件:
export class AppComponent {
showEditTable = false;
editRowId: any = '';
selectedRow;
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
editTableRow(val) {
console.log('click event started val = ' + val);
this.editRowId = val;
console.log('click event ended with val = ' + val);
}
}
我希望结果是可以内联编辑最后 2 列的表格,同时我可以将修改后的数据发送到后端
【问题讨论】:
-
“数据”中没有值吗?
-
使用简单的 td tr 获取数据时没有问题,我可以填充值但不能使用 mat 表
-
对不起我的评论(我读得太快了你的问题)。在 mat-table 中是相似的,只是与 ng-container “玩”,看我的回答
标签: html css angular typescript angular-material