I have an array like
[
{
"PermissionRoleModule":{
"id":1,
"legend":"businessModule",
"group":[
{
"PermissionRoleGroup":{
"id":1,
"permission":{
"controleType":"ff",
"id":2,
"key":"create Business"
},
"roles":[
{
"id":1,
"name":"self"
},
{
"id":2,
"name":"other"
}
]
}
},
{
"PermissionRoleGroup":{
"id":1,
"permission":{
"controleType":"ff",
"id":2,
"key":"edit business"
},
"roles":[
{
"id":1,
"name":"self"
},
{
"id":2,
"name":"other"
}
]
}
}
]
}
},
{
"PermissionRoleModule":{
"id":2,
"legend":"PanicModule",
"group":[
{
"PermissionRoleGroup":{
"id":1,
"permission":{
"controleType":"ff",
"id":2,
"key":"create panic"
},
"roles":[
{
"id":1,
"name":"self"
},
{
"id":2,
"name":"other"
}
]
}
},
{
"PermissionRoleGroup":{
"id":1,
"permission":{
"controleType":"ff",
"id":2,
"key":"edit panic"
},
"roles":[
{
"id":1,
"name":"self"
},
{
"id":2,
"name":"other"
}
]
}
}
]
}
}
]
and my view is like shown in attatchment
when I click submit button I am expecting json like
[
{
"name":"aaa",
"description":"das",
"permission":[
{
"permission_id":1,
"relation":2
}
]
}
]
How to build form group for this case using reactive forms
I tried like this
component.ts
roleForm: FormGroup;
formField: any;
validateForm() {
this.formField['rolename'] = new FormControl('', Validators.compose([Validators.required]))
this.formField['roledescription'] = new FormControl('', Validators.compose([Validators.required]))
this.form_objects.forEach(element => {
if (element.group) {
element.group.forEach(PermissionRoleGroup => {
this.formField[PermissionRoleGroup.permission.key] = new FormControl({value:''}, Validators.compose([Validators.required]))
if (PermissionRoleGroup.roles) {
PermissionRoleGroup.roles.forEach(role => {
this.formField[role.key] = new FormControl('', Validators.compose([Validators.required]))
});
}
})
}
});
this.roleForm = this.fb.group(this.formField);
}
html
<div class="form-row" *ngFor="let element of form_objects; let i = index; ">
<table class="table table-bordered">
<tr *ngFor="let permissionRoleGroup of element.group;let j= index;">
<table class="table table-bordered" style="margin-bottom: 0px;">
<td width="40%">
<label class="font-light">
<input type="checkbox"
[id]="permissionRoleGroup.permission.key"
[formControlName]="permissionRoleGroup.permission.key"
[value] = "permissionRoleGroup.permission.value">
{{permissionRoleGroup.permission.label }}-{{permissionRoleGroup.permission.ischecked}}
</label>
</td>
<td width="15%" *ngFor="let role of permissionRoleGroup.roles">
<label class="font-light">
<input type="checkbox"
[value] = "role.value"
[formControlName]=" role.key">
{{role.label}}-{{role.ischecked}}
</label>
</td>
</table>
</tr>
</table>
</div>
with this I am able to build form , during submission, my form is not creating json .
This is plunker link https://plnkr.co/edit/h2VuBw4uITi6czn98ViS?p=preview
As i am beginner in angular-2 so please help me out.
I have created the following function:
Simply pass an empty form and your object. It will build your form based on the object you passed:
If you want to build it yourself, you have to do it manually... I would recommend you this article: https://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html
I would create some property like
permissionGroups
and flatten your datathen i would build
formGroup
like this:and prepare html as follows:
And when you will submit the form you need to convert form value to your desired result something like this:
and you will see the output like:
You can play with the code in Plunker Example
See also