I am making angular 6 application where i am using Angular dynamic form
Here i have made a nested input fields in which there will be two input textboxes at the initial stage and on click the add button the next two input boxes will get append on each click on add button.
Everything works fine here regarding it..
Here i have used the values in question-service.ts as,
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "project_name",
label: "Project Name",
type: "text",
value: '',
required: true,
order: 1
}),
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "project_desc",
label: "Project Description",
type: "text",
value: '',
required: true,
order: 2
}),
new ArrayQuestion({
key: 'myArray',
value: '',
order: 3,
children: [
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "property_one",
label: "Property One",
type: "text",
value: '',
required: true,
order: 3
}),
new TextboxQuestion({
elementType: "textbox",
class: "col-12 col-md-4 col-sm-12",
key: "property_two",
label: "Property Two",
type: "text",
value: '' ,
required: true,
order: 4
})
]
})
Which i need to change like the data should be from json for each like,
jsonData: any = [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_name",
"label": "Project Name",
"type": "text",
"value": "",
"required": true,
"order": 1
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_desc",
"label": "Project Description",
"type": "text",
"value": "",
"required": true,
"order": 2
},
{
"elementType": "array",
"key": "myArray",
"value": "",
"order": "3",
"children": [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "property_one",
"label": "Property One",
"type": "text",
"value": "",
"required": true,
"order": 3
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "property_two",
"label": "Property Two",
"type": "text",
"value": "",
"required": true,
"order": 4
}
]
}
];
Stackblitz without JSON:
https://stackblitz.com/edit/angular-x4a5b6-xcychx
Stackblitz with JSON:
https://stackblitz.com/edit/angular-x4a5b6-u6ecpk
The same scenario which happens in stacblitz link without json needs to happen while loading JSON..
I have given the following inside getQuestions()
like,
getQuestions() {
console.log(this.jsonData);
let questions: any = [];
this.jsonData.forEach(element => {
if (element.elementType === 'textbox') {
questions.push(new TextboxQuestion(element));
} else if (element.elementType === 'array') {
questions.push(new ArrayQuestion(element));
}
});
return questions.sort((a, b) => a.order - b.order);
}
}
For normal textbox its working but whereas for the children its not working on clicking add button (textboxes were not displayed), the child textboxes not getting added.
Kindly help me to achieve the same result that happens in link 1 also needs to happen while using JSON in link 2 .. And also kindly dont include any third party libraries inside everything is going in core angular.