I have DTO in my code as follow :
export class MatrixDTO implements cols {
constructor() {
this.name = "";
this.items = [];
this.customRow = false;
this.label="";
this.customCol=false;
}
name : string;
items :Array<cols>;
customRow:boolean;
label: string;
customCol:boolean;
}
interface cols {
label: string;
customCol:boolean;
}
Forms init code as follow :
initilazieForm(): void {
this.matrixForm = this.fb.group({
matrix: this.fb.array([], [Validators.required]),
});
}
How should I declare my form array matrix so I can add new rows and columns also and same reflect when I save form.
Edited as per comments :
I am looking for a final FormArray to be something like this and where customRow or customCol is false , I want to show label instead of text boxes.
[
{
"label": "Apple",
"customRow": false,
"values": [
{
"label": "30",
"customCol": false
},
{
"label": "30",
"customCol": false
},
{
"label": "30",
"customCol": false
}
]
},
{
"label": "Bannana",
"customRow": false,
"values": [
{
"label": "50",
"customCol": false
},
{
"label": "60",
"customCol": false
},
{
"label": "70",
"customCol": false
}
]
},
{
"label": null,
"customRow": true,
"values": [
{
"label": "",
"customCol": true
},
{
"label": "",
"customCol": true
},
{
"label": "",
"customCol": true
}
]
}
]
Edit for patch value error :
#1 JSON I am getting through service while come on same form for edit : you can check only matrix node JSON ,rest is related to other controls related values.
{
"name":"rahul",
"brand":"Brand",
"market":"Market",
"productName":"yVW4LHi1_Uazy2_aKVfaSw",
"isArchived":false,
"isCustom":false,
"matrix":[
{
"label":"Apple",
"customRow":false,
"items":[
{
"label":"10",
"customCol":false
},
{
"label":"20",
"customCol":false
},
{
"label":"30",
"customCol":false
},
{
"label":"40",
"customCol":true
}
]
},
{
"label":"Bannana",
"customRow":false,
"items":[
{
"label":"12",
"customCol":false
},
{
"label":"22",
"customCol":false
},
{
"label":"32",
"customCol":false
},
{
"label":"22",
"customCol":true
}
]
},
{
"label":"Test",
"customRow":true,
"items":[
{
"label":"1",
"customCol":true
},
{
"label":"2",
"customCol":true
},
{
"label":"3",
"customCol":true
},
{
"label":"4",
"customCol":true
}
]
}
]
}
#2 and code I am trying to use to patch values is as follow :
setFormValues(data: any) {
console.log(data);
this.specForm.patchValue({
name: data.name,
Brand: data.Brand,
Market : data.Market,
productName : '',
IsArchived : data.IsArchived,
OriginId : data.OriginId,
IsCustom : data.IsCustom,
specs: this.fb.array([], [Validators.required]),
matrix: data.matrix.map((x:any) => this.rowGroup(x))
});
}