How can i add several columns if i add the "click here to add transportation field" like i want to add driver, contact number etc.... Once you click the modal, it will show you some options on what field to add. Please check the code link that I've made. here's also my codes below. Please check also the image here
CHECK THIS LINK https://stackblitz.com/edit/dynamic-columns-reactive-forms-hj5nur?file=app/app.component.ts
openModal(template: TemplateRef < any > ) {
this.modalRef = this.modalService.show(template);
}
initGroup() {
let rows = this.addForm.get('rows') as FormArray;
rows.push(this.fb.group({
description: [null, Validators.required],
pickup_area: [null, Validators.required],
pickup_time: [null, Validators.required],
sign_board: [null, Validators.required],
driver: [null],
contact_number: [null],
transportation_unit: [null],
special_instructions: [null],
}))
}
You'll have to create a new
FormGroup
for the modal fields and then use this form'svalue
to show or hide those specific fields from the mainform
Also, since you're already creating those fields in the main form but assigning them
undefined
, it won't matter if they're actually present on the form or not if the form's value is all you care about.So following these steps would fix your issue:
First: Create a new FormGroup for your Modal
Second: Create a variable on the class for configuring what needs to be visible/hidden from the main form:
Third: Read the value of the modal form on it's submission and assign it to
formControlsVisibilityConfig
. Also, hide the modal:Fourth: Bind the form to the template:
Here's an Updated StackBlitz for your ref.
here is the edited stackblitz : https://stackblitz.com/edit/dynamic-columns-reactive-forms-d3xsnf?file=app%2Fapp.component.html
this link contains validation too :
https://stackblitz.com/edit/dynamic-columns-reactive-forms-ytiiqp?file=app/custom-required-validator.ts