I'm new to Angular and I'm trying to create a dynamic table where you can add new columns, rows, and data. The issue that I'm having is with getting the input data from a form group and pushing it into an object that the table is reading from.
The initial group looks like this, and each control appears as a column.
input = new FormGroup({
name: new FormControl(),
age: new FormControl(),
height: new FormConrol()
});
So when you add a column a new control is added using 'input.addControl()'.
Without adding any new columns I could easily push this to the data source but saying,
this.dataSource.push({name: this.input.value.name}) // and so forth
But when new columns are added I have no idea how to push the values to this dataSource variable, since they could be called anything.
Is there a way to say iterate the form groups values and push them like that or something along those lines... Any help would be greatly appreciated.
What you actually need is a
FormArray
and notFormGroup
.FormArray
works more like anArray
. You canpush
values and remove values from itIn the below example I am implementing
FormBuilder
to generate theFormGroup
andFormArray
In the HTML
See this stackblitz demo