Я отображаю массив форм внутри mat-table
:
<mat-table #table [dataSource]="dataSource" class="example-table"
*ngIf="dataSource">
<ng-container matColumnDef="invoice_id">
<mat-header-cell *matHeaderCellDef>Invoice#</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.invoice_id }}</mat-cell>
</ng-container>
<ng-container matColumnDef="update_status">
<mat-header-cell *matHeaderCellDef>Update Status</mat-header-cell>
<!-- <mat-cell *matCellDef="let row">{{ row.date_modified }}</mat-cell> -->
<mat-cell *matCellDef="let row; let i = index;"
formArrayName="status_array">
<div [formGroupName]="i">
<mat-form-field>
<mat-label>Status</mat-label>
<mat-select id="receipt_status"
formControlName="receipt_status" placeholder="Status">
<mat-option (click)="onChange(row, i, 'ms', status.id)"
*ngFor="let status of ReceiptStatus;"
[value]="status.id">
{{status.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
<mat-paginator #paginator [length]="length" [pageSize]="pageSize"
[showFirstLastButtons]="true" [hidePageSize]="true"
(page)="loadData($event)" *ngIf="dataSource && length > 0">
</mat-paginator>
</div>
В сценарии .ts
:
внутри ngOnInit:
ngOnInit {
this.createStatusArray();
}
createStatusArray(){
this.statusArrayForm = this.fb.group({
'status_array': this.createArray()
})
}
createArray(): FormArray {
return new FormArray(this.dataSource.data.map(item => new FormGroup({
receipt_status: new FormControl(item['is_active']),
})));
}
Источник данных устанавливается из вызова API:
if(response && response.status == "success"){
this.receiptMsg = false;
this.receiptsArray = response.response;
this.length = this.receiptsArray.length;
this.dataSource = response.response;
// console.log(this.dataSource)
setTimeout(() => {
this.dataSource.paginator = this.paginator;
}, 50);
Статусы, добавленные в *ngFor
:
ReceiptStatus = [
{id: 0, name: 'Inactive'},
{id: 1, name: 'Active'}
];
Я продолжал получать следующие ошибки:
ОШИБКА Ошибка: не удается найти элемент управления с путем: 'status_array -> 1'