使用 angular 表单更改表单元素

Change form elements using angular form

我正在制作 angular 动态表单,表单元素通过 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 的文本框的项目名称没有变化。

然后在接下来我们有一个下拉列表,其中 key 作为项目,从这里只有需求开始..

在选项中,如果我选择Show Project Description,那么Project Description文本框需要显示,另外两个project_levelproject_members需要隐藏格式。 .

同样,如果我选择 Show Project Level,则需要单独显示 Project Level 下拉菜单,而 Project DescriptionProject Members 需要隐藏..

同理 Project Members..

那么如何根据项目下拉值的选择更改form-elements

工作示例 在这里 https://stackblitz.com/edit/angular-x4a5b6-5ys5hf

请帮助我根据 项目 下拉列表中的选择隐藏其他元素,单独使用 angular 动态表单..

您需要对代码进行少量更改。

  1. 更改 json 以便选项键与控制键匹配。

    ... { "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 }, ...

  2. 在你的表单中添加一个 *ngIf 到 app-question 组件,它将执行一个传递问题的函数,这个函数将保存隐藏给定问题的逻辑。

    <app-question *ngIf="isShowQuestion(question)" [question]="question" [form]="form"> </app-question>

  3. 函数逻辑将检查问题是否是您要隐藏的控件之一,如果是,它将检查下拉列表的值 'option to display' 是否匹配,如果匹配的话会显示问题,否则会隐藏问题。

    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 项目,所以你可以看到它的实际效果 here

正如@benshabatnoam 所说,你唯一需要做的就是改变你的动态形式问题以包括一些类似的东西

<div [formGroup]="form" *ngIf="?????">

您可以使用@Benshabatnoam 所说的条件,但我建议您多一些 "parametrizable"

假设您的 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",这个字段总是显示。

嗯,你必须再努力,你必须改变问题-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;
  }

您可以看到您的 forked stackblitz