简体   繁体   English

带有数组的反应形式的primeng复选框

[英]primeng checkbox with reactive form with array

I am trying to add my array of object to map the primeng checkbox and would like to get the values for selected check boxes.我正在尝试添加我的对象数组以映射primeng 复选框,并希望获取所选复选框的值。

I have tried FormControlName but it it's throwing undefined after submitting.我试过 FormControlName 但它在提交后抛出 undefined 。

below is the rough code下面是粗略的代码

data = [
    { type: dropdown
      text: 'drop',
      num: 1.23,
      options: [
      {
       value=1,
       text= 'drop1
      },{
       value=2,
       text= 'drop2
      }
      ]
    },
    { type: checkbox
      text: 'check',
      num: 1.23,
      options: [
      {
       value=1,
       text= 'check1
      },{
       value=2,
       text= 'check2
      }
      ]
    },
    { type: radio
      text: 'radio',
      num: 1.23,
      options: [
      {
       value=1,
       text= 'radio1
      },{
       value=2,
       text= 'radio2
      }
      ]
    },
  ];

Template:模板:

<form [formGroup]="group">

  <div *ngFor="let d of data">
  <div *ngSwitchCase = "checkbox">
    <p-checkbox *ngFor="let check of options"  [value]="check.value" [formControlName]="check.text"></p-checkbox>
    </div>
    <div *ngSwitchCase = "dropdown">
  <p-dropdown *ngFor="let drop of options" [value]="drop.value" [formControlName]="d.text"> {{drop.text}}
   </p-dropdown>
  </div>
   <div *ngSwitchCase = "radio">
    <p-radioButton  *ngFor="let radio of options"[value]="radio.value" [formControlName]="d.text"></p-radioButton >
  </div>
  </div>
 </form>

How I can get the reference of my control and values the same for drop down and check boxes.我如何获得我的控件的引用,并且下拉和复选框的值相同。

How to get the values for dynamic forms?如何获取动态表单的值?

for reactive dynamic form first thing we have to generate the formGroup base of the form control data对于反应式动态表单,我们首先必须生成表单控件数据的 formGroup 基础

getFormGroup method will return a formGroup object by loop over the data and create a form controls with name base of the text value . getFormGroup 方法将通过循环数据返回一个 formGroup 对象,并创建一个具有text值名称基础的表单控件。

getFormGroup() {
    
    const formControls = this.data.reduce( (controls , f:FormControl)=>{

      controls[f.text] = this.formBuilder.control(null);
      return controls;

    },{});

    return this.formBuilder.group(formControls)
  }

after we generate the form now we can render the form controls on the template在我们生成表单之后,我们可以在模板上呈现表单控件

<form [formGroup]="form">

    <div *ngFor="let d of data">

        <ng-container [ngSwitch]="d.type">

            <label for="">{{d.text}}</label>
            <div *ngSwitchCase="'checkbox'">
                <p-checkbox *ngFor="let check of d.options" [label]="check.label" [value]="check.value"
                    [formControlName]="d.text"></p-checkbox>
            </div>

            <div *ngSwitchCase="'dropdown'">
                <p-dropdown [options]="d.options" [formControlName]="d.text">
                </p-dropdown>
            </div>

            <div *ngSwitchCase="'radio'">

                <p-radioButton *ngFor="let radio of d.options" [name]="d.text" [label]="radio.label"
                    [value]="radio.value" [formControlName]="d.text">
                </p-radioButton>

            </div>

        </ng-container>
    </div>
</form>

stackblitz demo 🚀 stackblitz 演示 🚀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM