注意:我成功地在经典 HTML 表中执行 FormArray,如下所示。我想在 Angular Material 表中有一个 FormArray 并用数据填充它。我尝试了与经典 HTML 表相同的方法,但由于错误“找不到带有 id 'name'' 的列” ,我无法编译它
<div class="form-group">
<form [formGroup]="myForm" role="form">
<div formArrayName="formArrList">
<table>
<tr>
<th>Name</th>
<th>School</th>
</tr>
<tr *ngFor="let list of myForm.get('formArrList').controls;let i = index" [formGroupName]="i">
<td>
<div class="col-sm-6">
<input class="form-control" type="text" formControlName="name"/>
</div>
</td>
<td>
<div class="col-sm-6">
<input class="form-control" type="text" formControlName="school"/>
</div>
</td>
</tr>
</table>
</div>
</form>
我尝试在我的 Angular Material Table 中有一个 FormArray 这是我的 HTML 文件
<div>
<form [formGroup]="myForm" role="form">
<ng-container formArrayName="formArrList">
<mat-table #table [dataSource]="myDataSource">
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<ng-container *ngFor="let detailsItems of myForm.get('formArrList').controls;let i = index" [formGroupName]="i">
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-form-field class="" hideRequiredMarker>
<input matInput formControlName="name" type="text" class="form-control"
autocomplete="off"
placeholder="name">
</mat-form-field>
</mat-cell>
</ng-container>
<ng-container matColumnDef="school">
<mat-header-cell *matHeaderCellDef>School</mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-form-field class="" hideRequiredMarker>
<input matInput formControlName="school" type="text" class="form-control"
autocomplete="off"
placeholder="school">
</mat-form-field>
</mat-cell>
</ng-container>
</ng-container>
</mat-table>
</ng-container>
</form>
这是我的 .TS 文件的一部分
@Component(..)
export class DemO implements OnInit {
displayedColumns = ['name', 'school'];
myForm: FormGroup;
formArrList: FormArray;
myDataSource: DataSource;
dummyData: Element[] = [];
ngOnInit(): void {
//init form arrayTree
this.myForm = this.formBuilder.group({
'formArrList': new FormArray([])
});
}
initTreeFormArray(name: string, school: string) {
return this.formBuilder.group({
'name': [code_any,],
'school': [prio,]
});
}
renderTableOnButtonClick(){
const control = <FormArray>this.treeForm.controls['formArrList'];
control.push(this.initTreeFormArray("DummyName", "DummySchool", element.name));
this.dummyData.push({name: "DummyName", school: "DummySchool"});
this.myDataSource = new sDataSource(this.dummyData);
}