[英]Change form elements using angular form
我正在使用通过JSON加载的表单元素制作角度动态表单。
表单元素生成工作正常,但是我需要根据从下拉列表中选择的选项来更改表单元素。
生成表单元素的JSON
jsonData: any = [
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_name",
"label": "Project Name",
"type": "text",
"value": "",
"required": false,
"minlength": 3,
"maxlength": 20,
"order": 1
},
{
"elementType": "dropdown",
"key": 'project',
"label": 'Choose option to display',
"options": [
{ "key": 'description', "value": 'Show Project Description' },
{ "key": 'level', "value": 'Show Project Level' },
{ "key": 'members', "value": 'Show Project Members' }
],
"order": 2
},
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_desc",
"label": "Project Description",
"type": "text",
"value": "",
"order": 3
},
{
"elementType": "dropdown",
"key": 'project_level',
"label": 'Choose Project Level',
"options": [
{ "key": 'low', "value": 'Low' },
{ "key": 'medium', "value": 'Medium' },
{ "key": 'high', "value": 'High' }
],
"order": 4
},
{
"elementType": "dropdown",
"key": 'project_members',
"label": 'Choose Project Member',
"options": [
{ "key": 'developer', "value": 'Developer' },
{ "key": 'leader', "value": 'Leader' },
{ "key": 'manager', "value": 'Manager' }
],
"order": 5
}
];
基于上述JSON,将生成元素。
在这里,您可以看到Order 1
文本框的项目名称没有更改。
然后在下一步中,我们有一个键为project的下拉列表,从这里仅需求开始。
在选项中,如果我选择Show Project Description
,则需要显示Project Description
文本框,而其他两个project_level
和project_members
必须为隐藏格式。
同样,如果我选择“ Show Project Level
则仅需要显示“ Project Level
下拉列表,并且需要隐藏“ Project Description
和“ Project Members
。
对于Project Members
以同样的方式..
那么如何根据项目下拉值的选择来更改form-elements
?
相同的工作示例在这里https://stackblitz.com/edit/angular-x4a5b6-5ys5hf
请帮我隐藏基于单从使用的角度动态表单项目下拉列表中选择其他元素..
正如@benshabatnoam所说,您唯一需要做的就是将您的dinamic-form-question更改为包括
<div [formGroup]="form" *ngIf="?????">
您可以使用类似@Benshabatnoam这样的条件,但我建议您更多一些“可参数化”
想象一下,您的json有一个新属性“ visible”,它是一个具有两个属性(字段和值)的对象。 因此,您的元素“ project_desc”可以像
{
"elementType": "textbox",
"class": "col-12 col-md-4 col-sm-12",
"key": "project_desc",
"label": "Project Description",
"type": "text",
"value": "",
"order": 3,
"visible":{"field":"project","value":'description'} //<--add this "property"
},
所以动态形式的问题可以像
<div [formGroup]="form"
*ngIf="!question.visible ||
form.get(question.visible.field).value==question.visible.value">
...
</div>
请注意,我包含条件(!question.visible),因此,如果不将属性“ visible”赋予一个字段,则始终显示该字段。
好吧,您必须做更多的工作,必须更改question-base.ts才能接纳此新属性
export class QuestionBase<T> {
value: T;
...
visible:any; //<--ad this property
constructor(options: {
value?: T,
...
visible?:any, //In constructor include "visible" too
..
}){
this.value = options.value;
...
this.visible = options.visible;
}
您可以看到分叉的堆叠闪电战
您只需对代码进行一些更改。
更改json,以便options键与control键匹配。
... { "elementType": "dropdown", "key": 'project', "label": 'Choose option to display', "options": [ { "key": 'project_desc', "value": 'Show Project Description' }, { "key": 'project_level', "value": 'Show Project Level' }, { "key": 'project_members', "value": 'Show Project Members' } ], "order": 2 }, { "elementType": "textbox", "class": "col-12 col-md-4 col-sm-12", "key": "project_desc", "label": "Project Description", "type": "text", "value": "", "order": 3 }, ...
在您的表单中,向app-question组件添加* ngIf,它将执行向其传递问题的函数,该函数将保留用于隐藏给定问题的逻辑。
<app-question *ngIf="isShowQuestion(question)" [question]="question" [form]="form"> </app-question>
函数逻辑将检查问题是否为您要隐藏的控件之一,如果是,它将检查下拉菜单“显示选项”的值以进行匹配,如果匹配则显示问题,否则将隐藏问题。
isShowQuestion(question: QuestionBase<any>): boolean { // If one of the controls you want to hide if (question.key === 'project_desc' || question.key === 'project_level' || question.key === 'project_members') { // if the option to display have value and it is this question that show it else don't show it return !this.form.controls['project'].value || this.form.controls['project'].value === question.key; } else { // if not, always display return true; } }
我已经分叉了您的stackblitz项目,因此您可以在此处查看它的运行情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.