I have to dynamically create a form with data received from the server with the option of adding and removing input fields for each existing input fields.
This is the data i receive from the server
documents = [{id: 1, service_item_id: 311101, name: 'document 1'}, {id: 2, service_item_id: 311102,, name: 'document 2'}, {id: 3, service_item_id: 311103,, name: 'document 3'}, {id: 4, service_item_id: 311104,, name: 'document 4'}]
I have created a function to create the form
createControls(controls) {
console.log(controls);
for (let control of controls) {
const newFormControl = new FormControl();
this.myForm.addControl(control.service_item_id, newFormControl);
}
}
this.createControls(this.documents);
And in the Html I have created this
<form [formGroup]="myForm" (ngSubmit)="submitForm()">
<div *ngFor="let c of documentation; let index = index">
<label>
{{ c.name}}
</label>
<input type="number" [formControlName]="c.service_item_id" [placeholder]="c.name" />
<button class="btn btn-primary btn-sm" (click)="add(c.service_item_id)">Add</button>
<button class="btn btn-primary btn-sm" (click)="remove(c.service_item_id, index)">Remove</button>
</div>
<button type="submit" class="btn btn-primary btn-small">Submit</button>
</form>
My add and remove functions are
add(item): void {
(this.myForm.get(item) as FormArray).push(
this.fb.control(null)
);
}
remove(item, index) {
(this.myForm.get(item) as FormArray).removeAt(index);
}
Only the form is getting created but add and remove buttons are not working.
Please do help me resolve this issue. Thanks in advance
I think what will work best for your situation is to use
FormArray. WithFormArrayyou can push and remove items just like you would do with a normal arrayto add items we use
pushwhile to remove items we useremoveAtBelow is the html
Edit
To add items below there clicked button we can implement
insert()I have updated the below Demo to reflect this
See Demo