I want to create a table like below, which is a editable form table in angular 2+. From user I will get the adult count, child count and infant count.
**# First name Last name Age**
Adult 1 fn1 Ln16 Dec 27
Adult 2 fn2 Ln15 Dec 27
Adult 3 fn3 Ln14 Dec 27
Child 1 fn4 Ln13 Dec 27
Child 2 fn5 Ln12 Dec 27
Infant 1 fn6 Ln11 Dec 27
By the use input I will build the form like
ngOnInit() {
this.travellerForm=this.formBuilder.group({
adults: this.formBuilder.array([]),
children: this.formBuilder.array([]),
infant: this.formBuilder.array([])
})
const control1 = this.travellerForm.controls['adults'] as FormArray;
for (let i = 0; i < this.adultCount; i++) {
control1.push(this.adultForm());
}
const control2 = this.travellerForm.controls['children'] as FormArray;
for (let i = 0; i < this.childCount; i++) {
control2.push(this.childForm());
}
const control3 = this.travellerForm.controls['infant'] as FormArray;
for (let i = 0; i < this.infantCount; i++) {
control3.push(this.infantForm());
}
}
adultForm() {
let d = new Date();
d.setFullYear(d.getFullYear() - 12);
return this.formBuilder.group({
first_name : [''],
last_name : [''],
dob: [new Date(d)],
});
}
childForm() {
let d = new Date();
d.setFullYear(d.getFullYear() - 2);
return this.formBuilder.group({
first_name : [''],
last_name : [''],
dob: [new Date(d)],
});
}
infantForm() {
let d = new Date();
d.setFullYear(d.getFullYear() - 2);
return this.formBuilder.group({
first_name : [''],
last_name : [''],
dob: [new Date(d)],
});
}
The html is like
<form [formGroup]="travellerForm" class="form-horizontal">
<div class="table-responsive">
<table class="table table-bordered table-striped table-highlight">
<thead>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>DOB</th>
</thead>
<tbody>
<tr *ngFor="
let Y of travellerForm['controls’].children[
'controls'
];
let ix = index
" [formGroupName]="ix">
<td>Adult 1</td>
<td><input type="text" class="form-control" /></td>
<td><input type="text" class="form-control" /></td>
<td><input type="text" class="form-control" bsDatepicker #dp1="bsDatepicker" /></td>
</tr>
<tr *ngFor="
let Y of travellerForm['controls'].adults[
'controls'
];
let ix = index
" [formGroupName]="ix">
<td>Adult 1</td>
<td><input type="text" class="form-control" /></td>
<td><input type="text" class="form-control" /></td>
<td><input type="text" class="form-control" bsDatepicker #dp1="bsDatepicker" /></td>
</tr>
<tr *ngFor="
let Y of travellerForm['controls’].infants[
'controls'
];
let ix = index
" [formGroupName]="ix">
<td>Adult 1</td>
<td><input type="text" class="form-control" /></td>
<td><input type="text" class="form-control" /></td>
<td><input type="text" class="form-control" bsDatepicker #dp1="bsDatepicker" /></td>
</tr>
</tbody>
</table>
</div>
</form>
It is giving “Cannot find control with unspecified name attribute” error. How can I solve it and have a table like above.