I'm trying to build an inline editable table using the latest material+cdk for angular.
Question
How can I make mat-table use
[formGroupName]
so that the form fields can be referenced by its correct form path?
This is what I got so far: Complete StackBlitz example
Template
<form [formGroup]="form">
<h1>Works</h1>
<div formArrayName="dates" *ngFor="let date of rows.controls; let i = index;">
<div [formGroupName]="i">
<input type="date" formControlName="from" placeholder="From date">
<input type="date" formControlName="to" placeholder="To date">
</div>
</div>
<h1>Wont work</h1>
<table mat-table [dataSource]="dataSource" formArrayName="dates">
<!-- Row definitions -->
<tr mat-header-row *matHeaderRowDef="displayColumns"></tr>
<tr mat-row *matRowDef="let row; let i = index; columns: displayColumns;" [formGroupName]="i"></tr>
<!-- Column definitions -->
<ng-container matColumnDef="from">
<th mat-header-cell *matHeaderCellDef> From </th>
<td mat-cell *matCellDef="let row">
<input type="date" formControlName="from" placeholder="From date">
</td>
</ng-container>
<ng-container matColumnDef="to">
<th mat-header-cell *matHeaderCellDef> To </th>
<td mat-cell *matCellDef="let row">
<input type="date" formControlName="to" placeholder="To date">
</td>
</ng-container>
</table>
<button type="button" (click)="addRow()">Add row</button>
</form>
Component
export class AppComponent implements OnInit {
data: TableData[] = [ { from: new Date(), to: new Date() } ];
dataSource = new BehaviorSubject<AbstractControl[]>([]);
displayColumns = ['from', 'to'];
rows: FormArray = this.fb.array([]);
form: FormGroup = this.fb.group({ 'dates': this.rows });
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.data.forEach((d: TableData) => this.addRow(d, false));
this.updateView();
}
emptyTable() {
while (this.rows.length !== 0) {
this.rows.removeAt(0);
}
}
addRow(d?: TableData, noUpdate?: boolean) {
const row = this.fb.group({
'from' : [d && d.from ? d.from : null, []],
'to' : [d && d.to ? d.to : null, []]
});
this.rows.push(row);
if (!noUpdate) { this.updateView(); }
}
updateView() {
this.dataSource.next(this.rows.controls);
}
}
Problem
This wont work. Console yields
ERROR Error: Cannot find control with path: 'dates -> from'
It seems as if the [formGroupName]="i"
has no effect, cause the path should be dates -> 0 -> from
when using a formArray.
My current workaround: For this problem, I've bypassed the internal path lookup (formControlName="from"
) and use the form control directly: [formControl]="row.get('from')"
, but I would like to know how I can (or at least why I cannot) use the Reactive Form preferred way.
Any tips are welcome. Thank you.
Since I think this is a bug, I've registered an issue with the angular/material2 github repo.
Create a function that calculates the actual index.
You can get the
pageSize
andpageIndex
from the paginator. Then, in the template use this function:A little late to the party but I managed to get it working without relying on the index. This solution also supports filtering etc from the
MatTableDataSource
.https://stackblitz.com/edit/angular-material-table-with-form-59imvq
Component
HTML
here is the sample code
In Html:
Controller code:
I would use the index which we can get within
matCellDef
binding:Forked Stackblitz