I am using reactive forms and I have one text box and other item in form is matrix , which is basically array of objects.
Initilazation of form is done using following code :
initilazieForm(): void {
this.specForm = this.fb.group({
name: 'Enter specsheet name',
matrix: this.fb.array(this.mat, [Validators.required])
});
}
and this.mat is as following
createBaseMatrix(): MatrixDTO[] {
this.mat[this.mat.length] = new MatrixDTO();
this.mat[0].name = 'Basic Weight(lbs/3000ft)';
this.mat[0].customRow = false;
this.mat[0].items.push({ label: '30', customCol: false });
this.mat[0].items.push({ label: '35', customCol: false });
this.mat[0].items.push({ label: '40', customCol: false });
this.mat[this.mat.length] = new MatrixDTO();
this.mat[1] = new MatrixDTO();
this.mat[1].name = 'Basic Weight(g/m2)';
this.mat[1].customRow = false;
this.mat[1].items.push({ label: '34', customCol: false });
this.mat[1].items.push({ label: '42', customCol: false });
this.mat[1].items.push({ label: '68', customCol: false });
return this.mat;
}
I have two buttons on same form , which are used to insert custom row or columns ( text boxes ) , so that user can enter values.
but when I add custom row and columns , insert data into text boxes for each row or column and submit the forms those values doesn't reflect in this.mat as well on forms values
addRow() {
console.log('add row clicked');
this.mat[this.mat.length] = new MatrixDTO();
this.mat[this.mat.length - 1].name = '';
this.mat[this.mat.length - 1].customRow = true;
let colsCount: number = this.mat[0].items.length;
var i;
for (i = 0; i < colsCount; i++) {
this.mat[this.mat.length - 1].items.push({ label: '', customCol: true });
}
console.log(this.mat);
}
addColumn() {
this.mat.forEach(x => x.items.push({ label: '', customCol: true }));
console.log('add column clicked');
console.log(this.mat);
}
<form [formGroup]="specForm" (ngSubmit)="submitForm()">
<div class="container">
<div class="row">
<div class="col-9">
<label for="name">Enter specsheet name</label>
<input type="text" class="form-control" placeholder="Enter specsheet name" formControlName="name" id="name">
</div>
<div class="col-3">
<button class="btn btn-primary" type="submit">Save</button>
</div>
</div>
<div class="row">
<div class="col-6">
<a (click)="addRow()">Insert Custom
Row</a>
</div>
<div class="col-6">
<a (click)="addColumn()">Insert
Custom Column</a>
</div>
</div>
<div class="row">
<div class="col-12">
<div *ngFor="let row of mat" class="row">
<div class="col-3">
<span *ngIf="row.customRow===false">{{row.name}}</span>
<input *ngIf="row.customRow==true"
type="text" [value]="row.name">
</div>
<div *ngFor="let cols of row.items" class="col-1">
<span *ngIf="cols.customCol===false"
style="padding: 5px;">{{cols.label}}</span>
<input *ngIf="cols.customCol===true"
type="text" [value]="cols.label"
class="numtext">
</div>
</div>
</div>
</div>
</div>
</form>