I have an array of formGroup, where each element represents a form. Next I have a mat table, where what I want to do is to generate each tr (so each row) as a distinct form, so that the i-th row of the table is linked to the i-th formGroup. So inside the i-th row, each td element contains an input, and this input should be linked to a formControl which is inside the i-th formGroup.
So basically each row is a form, which can be submitted individually (each row also has a "Submit" button).
How can I accomplish this? Can you provide me a boiler plate I can work on?
Edit: here's what I tried so far: https://angular-dpwgzp.stackblitz.io
where I get "Error: formControlName must be used with a parent formGroup directive." Also, I have no Idea about where to put the tag in each row.
It's difficult help if we can't get access to the code.
In this stackblitz I put a simple example. See that we create a form Array like
myformArray = new FormArray([
new FormGroup({
name: new FormControl("uno"),
surname: new FormControl("one")
}),
new FormGroup({
name: new FormControl("dos"),
surname: new FormControl("two")
}),
new FormGroup({
name: new FormControl("tres"),
surname: new FormControl("three")
})])
The dataSource of the table is the formArray controls.
dataSource = this.myformArray.controls;
In this way, "element" is a FormGroup, so a cell can be like
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element">
<input [formControl]="element.get('name')">
</td>
</ng-container>
See that we use [formControl]. That's. A mat-table iterate over myformArray.controls, that is only an array of FormGroups. The FormGroup is this "element", so element.get('...') is a FormControl. It's the reason we can use [formControl]=element.get('...')
it's like we write not mat-table
<div *ngFor="let element of myformArray.controls">
<input [formControl]="element.get('name')">
<input [formControl]="element.get('surname')">
</div>