I got a dynamic table that can add rows to it. How do I dynamically set the data in col 3? I am using form builder to create my forms and has a method that accepts to dynamically insert the rows in a table. When I write something in col1, I subscribe to change and compute the sum on the two cols to put it on the third col.
public sumRow() {
this.myForm
.get('rows')
.valueChanges
.subscribe((val) => {
// latest edit
val.foreach((item) => {
item.col3 = item.col1 + col2;
// But this one does not fill the col3 but
// it already giving the correct values
});
//loop to the val instead
// const ctrl = this.myForm.controls['rows'];
// ctrl.controls.forEach((field) => {
// const col1 = parseInt(field.get('col1').value, 10);
// const col2 = parseInt(field.get('col2').value, 10);
// const sum = col1 + col2;
// How to set these values to col3?
// Doesn't work: field.get('col3').setValue(sum);
});
}
public createForm() {
this.myForm = this.fb.group({
name: ['', Validators.required],
});
}
public pushRowItems(items) {
this.myForm.addControl('rows', this.fb.array([items]));
}
public initItemRows() {
return this.fb.group({
col1: 0,
col2: 0,
col3: 0,
});
}
public ngOnInit() {
this.createForm();
this.pushRowItems(this.initRowItems());
this.sumRow();
}