html - HTML 表格在表格列中正确使用和设置 mat-select
问题描述
我有这张桌子:
<table class="mat-elevation-z8">
<tr>
<th align="left" mat-header="nomeFile"> Nome file </th>
<th align="left" mat-header="note"> Note </th>
<th align="left" mat-header="tipoDocumento"> Tipo documento </th>
<th></th>
</tr>
<tr *ngFor="let documento of documenti" class="mat-row">
<td> {{documento.nome}} </td>
<td> {{documento.note}} </td>
<td> <mat-form-field class="col">
<mat-select formControlName="tipoDocumento" [(value)]="tipoDocumentoSelezionato">
<mat-option value="generico">Allegato Generico</mat-option>
<mat-option value="verbale">Verbale</mat-option>
<mat-option value="esito">Esito Analisi</mat-option>
</mat-select>
</mat-form-field> </td>
</tr>
</table>
这个实现的问题是,每当我在表的一行中选择一个选项时,表中的所有其他行都会为其 mat-select 设置相同的值。如果我为第一行选择选项“Esito Analisi”,这是输出:
我该如何解决这种行为?之后,如果我想保存数据,如何获取每一行的已选择选项的值?
解决方案
好的,解决方案非常愚蠢,但我希望它会对某人有所帮助。我设置[(value)]="documento.tipo"
而不是将其绑定到“通用”变量。所以最后我有:
<tr *ngFor="let documento of documenti" class="mat-row">
<td> {{documento.nome}} </td>
<td> {{documento.note}} </td>
<td> <mat-form-field class="col">
<mat-select [(value)]="documento.tipo">
<mat-option value="generico">Allegato Generico</mat-option>
<mat-option value="verbale">Verbale</mat-option>
<mat-option value="esito">Esito Analisi</mat-option>
</mat-select>
</mat-form-field> </td>
<td>
<button mat-button class="delete" (click)="rimuoviDaDocumenti(documento)">delete</button>
</td>
</tr>
并在方法“rimuoviDaDocumenti(documento)”中获取所选选项:
rimuoviDaDocumenti(documento){
console.log("Documento " + documento.tipo);
}
其中 documenti 是这个数组:
documenti = [
{
nome: "Doc 1",
note: "notes",
tipo: ""
},
{
nome: "Doc 2",
note: "notes",
tipo: ""
}
推荐阅读
- sass - 将相同的样式应用于父级和嵌套的子级
- excel-formula - 有没有办法在不单独选择每一列的情况下在 Excel 中跨行相乘?
- google-chrome - 使用 Linkedin 按钮申请不适用于 Chrome 80?
- javascript - 如何使用 JavaScript 获取给定周数的日期的天数
- azure - MySql 管理员用户失去了权限
- apache-kafka - 如何管理春季批处理作业中使用的KafkaItemReader中的偏移量,以防在读取消息中发生任何异常
- ios - 成功的 Firestore 写入未反映在数据中
- python-3.x - 在对话流中发送批量创建实体请求时遇到问题
- c# - 为什么我的程序在我取消 BackgroundWorker 后执行?
- php - docker php-fpm 容器的 Nginx 配置:“访问脚本 '/xxx/xxx/openapi/index.html' 已被拒绝(请参阅 security.limit_extensions)”