I am trying to make dynamic forms with Json data and the conditions should be selected from the options type selection from the JSON data. The JSON data structure are as follow. In this case for example, after selecting the the first element A Speditions GmbH from drop down the child content of this elements "label": "Subfield1", and another drop down options should be appear on the form.
My Typescript (Updated)
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { MatDialog, MatDialogRef } from '@angular/material';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
form:FormGroup;
formTemplate:any = form_template;
stepCfg = {
stepType: 'rahmendaten',
stepHeading: '',
stepIndex: 0,
steps: [],
firstStep: false,
lastStep: false,
onlyStep: false,
data: {},
htmlContent: ''
};
formDataObj = {};
ngOnInit() {
for (const prop of Object.keys(this.stepCfg.data)) {
this.formDataObj[prop] = new FormControl(this.stepCfg.data[prop].value);
this.formTemplate.push({
key: prop,
label: this.stepCfg.data[prop].label,
type: this.stepCfg.data[prop].type,
options: this.stepCfg.data[prop].options
});
}
this.form = new FormGroup(this.formDataObj);
}
}
const form_template = [{
"type": "select",
"label": "Spedition",
"options": [
{
"value": "sped1",
"label": "A Speditions GmbH",
"subfield": {
"type": "select",
"label": "Subfield1",
"options": [
{
"value": "subfieldvalue1",
"label": "101"
},
]
}
},
{
"value": "sped2",
"label": "Alf Scon GmbH"
},
{
"value": "sped3",
"label": "Barthtest"
},
]
}]
export default form_template
//HTml
<mat-card class="rahmendaten-container">
<h3 class="page__header">{{stepCfg.stepHeading}}</h3>
<div *ngIf="!!stepCfg.htmlContent" [innerHTML]="stepCfg.htmlContent">
</div>
<form [formGroup]="form" autocomplete="off" (ngSubmit)="onSubmit()">
<mat-card-content class="page">
<div class="page__container">
<div *ngFor="let prop of formTemplate" class="container__row">
<div [ngSwitch]="prop.type">
<div *ngSwitchCase="'select'">
<span [innerHTML]="prop.label" class="container__row__label">
</span>
<mat-form-field>
<mat-select>
<mat-option *ngFor="let option of prop.options"
[value]="option.value">{{option.label}}</mat-option>
</mat-select>
</mat-form-field>
</div>
</div>
</div>
</div>
</mat-card-content>
</form>
</mat-card>