angular - mat-table 重复数据数组的最后一项,而不是显示每个唯一的行
问题描述
这是表格代码:
<mat-table #table style="overflow: hidden" [dataSource]="list">
<ng-container matColumnDef="specifications">
<th mat-header-cell *matHeaderCellDef> Specifications </th>
<td mat-cell *matCellDef="let spec">
<mat-form-field fxFlex>
<textarea matInput name="specificationText" [(ngModel)]="spec.sortOrder"></textarea>
</mat-form-field>
</td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>
<button mat-raised-button mat-icon-button (click)="addRate()">
<mat-icon>add</mat-icon>
</button>
</th>
<td mat-cell *matCellDef="let specification">
0/250
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="['specifications', 'actions']"></tr>
<tr mat-row *matRowDef="let row; columns: tableColumns;"></tr>
</mat-table>
和打字稿代码:
@Component({
selector: 'build-activity-schedule',
templateUrl: './build-activity-schedule.component.html',
styleUrls: ['./build-activity-schedule.component.scss'],
providers: [BuildActivitiesLogicService, UsersLogicService]
})
export class BuildActivityScheduleComponent implements OnInit {
public formMode: FormMode;
public buildActivityRequiredLevels: Array<SelectListItem>;
@ViewChild(MatTable, undefined) public table1: MatTable<any>;
@Input() public buildActivityId: number;
public buildActivity: IBuildActivityMappedItem;
public selectedProduct: ISkinnyOfferingDto;
public list;
public taskAnalysisFormRequired: boolean;
public readonly tableColumns: string[] = [
'specifications',
'actions'
];
constructor(
public readonly navigationService: NavigationService,
protected readonly authService: AuthService,
private enumService: CbEnumService,
public readonly currentUser: CurrentUserService,
public readonly permissionsPermissions: PermissionsPermissions,
public readonly dialog: MatDialog,
private readonly buildActivitiesLogicService: BuildActivitiesLogicService
) {
this.formMode = FormMode.View;
this.buildActivityRequiredLevels =
this.enumService.getSelectList(BuildActivityRequiredLevel);
}
public ngOnInit(): void {
this.buildActivitiesLogicService
.getItem(this.buildActivityId)
.pipe(takeOne())
.subscribe(x => {
this.buildActivity = this.buildActivitiesLogicService
.$createMappedItem(BuildActivityMappedItem, x);
this.list = this.buildActivity.getNationalSchedule().buildActivityScheduleSpecificationsLineItems;
console.log(this.list);
});
}
}
该表显示 4 行,但表的行都是数组的最后一项。每行<textarea>
都有“4”。为什么它重复最后一行 4 次而不是只显示 4 个唯一行?
解决方案
问题是您name
对行 textarea 使用相同的 ngModel。因此,所有文本区域都绑定到单个表单控件,并且设置给该控件的最后一个值将应用于所有文本区域。尝试使用唯一的名称,例如:
<textarea matInput name="specificationText.{{spec.id}}" [(ngModel)]="spec.sortOrder"></textarea>
推荐阅读
- delphi - LiveBindings TObjectBindSourceAdapter
- r - R - 在创建具有列名的新变量时合并数据框
- c++ - C ++从输入文件读取到结构数组
- javascript - 组件事件处理程序接收列表中不同组件的道具
- roblox - Roblox:BindToClose 有效,PlayerRemoving 无效
- pandas - matplotlib 问题:如何擦除这个?
- php - Symfony - 进程运行抛出异常 - 在命令行上执行有效
- c# - 在 C# 中使用 ExtraParams 从 Stripe 的 API 中提取特定事件
- arrays - 根据条件切片,否则返回空数组
- r - 有没有办法通过 r 代码选择 Shift F3