2

我在 Angular 7 中创建了带有动态表单字段的动态表单,但我无法维护单个提交按钮,该按钮将返回所有动态表单的值。

动态形式multiple.html

<form novalidate (ngSubmit)="onSubmit(form.value)" [formGroup]="form">
  <div class="form-group">
  <h4>Details for {{group.groupName}}</h4>
  <div class='row flex-row'>
     <div class='col-md-6' *ngFor="let prop of objectProps">
        <div [ngSwitch]="prop.itemType">
           <div *ngSwitchCase="'Item'">
              <label [attr.for]="prop">{{prop.egineAttribue}}</label>
              <div [ngSwitch]="prop.componentType">
                 <div class='form-group__text'>
                    <input *ngSwitchCase="'Free Text'" 
                    [formControlName]="prop.key"
                    [id]="prop.key" [type]="prop.type" class="form-control">
                 </div>
                 <div *ngSwitchCase="'Radio Button'">
                    <label *ngFor="let option of prop.values">
                    <input
                    type="radio"
                    (change)='ind=i'
                    class="form-control"
                    [name]="prop.key"
                    [formControlName]="prop.key"
                    [value]="option">{{option}}
                    </label>
                 </div>
                 <div class='form-group__text select' *ngSwitchCase="'Drop-down'">
                    <select class="form-control" [formControlName]="prop.key">
                    <option *ngFor="let option of prop.values" [value]="option">
                    {{ option }}
                    </option>
                    </select>
                 </div>
              </div>
              <div class="error" *ngIf="form.get(prop.key).invalid && (form.get(prop.key).dirty || form.get(prop.key).touched)">
                 <div *ngIf="form.get(prop.key).errors.required">
                    {{ prop.label }} is required.
                 </div>
              </div>
           </div>
        </div>
     </div>
  </div>
  </div>
  </form>

动态形式multiple.ts

@Input() dataObject;
    @Input() group;
    form: FormGroup;    
ngOnInit() {
        this.offerconstructService.formReset.subscribe((val) => {
            if(val==='reset'){
            }
        })
        this.tempObj = this.dataObject;
        // remap the API to be suitable for iterating over it
        this.objectProps =
            Object.keys(this.dataObject)
                .map(prop => {
                    return Object.assign({}, { key: prop },
                        this.dataObject[prop]);
                });
        // setup the form
        const formGroup = {};
        for (let prop of Object.keys(this.dataObject)) {
            formGroup[prop] = new FormControl(this.dataObject[prop].value || '', this.mapValidators(this.dataObject[prop].validation));
        }
        this.form = new FormGroup(formGroup);
    }

canvas.component.ts

<p-dialog header="Add All Offer Details" closable="true" modal="true" showHeader="false" (onHide)="onHide()"
[resizable]="false" [responsive]="false" [(visible)]="display" [contentStyle]="{'height': '450px', 'max-height':'450px', 'width':'100% !important'}"
positionTop='150' positionRight='0' positionLeft='0'>
<div class="divider" style="margin-top: -1.5%"></div>
<div>
  <ul id="tabsv1" class="tabs">
    <li id="tabsv1-1" class="tab">
      <a tabindex="0">
        <a (click)='majorLine()' [ngClass]="{'active': majorLineItemsActive, 'tab__heading':true}">Add Major Line
          Details</a>
      </a>
    </li>
    <li id="tabsv1-2" class="tab">
      <a tabindex="0">
        <a (click)='minorLine()' [ngClass]="{'active': minorLineItemsActive, 'tab__heading':true}">Add Minor Line
          Details</a>
      </a>
    </li>
  </ul>
</div>
<div *ngIf='minorLineItemsActive'>
    <div *ngFor='let formGroupDataMinorItems of formGroupDataMinorItems'>
        <div *ngFor='let groupsMinor of formGroupDataMinorItems.groups'>
            <div class="row half-margin-bottom">
                <div class="col-md-12">
                 <dynamic-form-multiple [group]='groupsMinor' [groupName]='groupsMinor.groupName' [dataObject]="groupsMinor.listOfferQuestions"></dynamic-form-multiple>
          </div>
        </div>
        </div>
      </div>
</div>
<div *ngIf='majorLineItemsActive'>
  <div *ngFor='let formGroupData of formGroupData'>
    <div *ngFor='let groups of formGroupData.groups'>
        <div class="row half-margin-bottom">
            <div class="col-md-12">
             <dynamic-form-multiple [group]='groups' [dataObject]="groups.listOfferQuestions"></dynamic-form-multiple>
            </div>
      </div>
    </div>
  </div>
</div>

从 canvas.component.ts 我将 JSON 数据传递给 dynamic-form-multiple.html ,这将生成具有多个提交按钮的多个表单和字段,但我需要为所有表单设置一个提交按钮,它将返回所有表单在 JSON 结构中。

或者

有没有其他方法可以在一页上实现具有动态表单字段的多个表单?

4

1 回答 1

0

如果我正确理解您的问题,则无论何时提交任何一种表单,您都必须获取所有dynamic-form-multiple组件的表单数据canvas.component.ts

  • 听从提交事件dynamic-form-multiple。看起来像<dynamic-form-multiple (submit)="getAllFormsData"></dynamic-form-multiple>
  • 还定义一个方法dynamic-form-multiple.ts,您可以在其中迭代动态生成的表单并将表单数据作为 json 返回。让我们称之为getFormAsJson()
  • 在提交任何动态表单时都会调用的canvas.component.ts定义方法中。getAllFormsData()要获取所有动态表单数据,您可以按如下方式进行。
export class CanvasComponent {
  @ViewChildren("dynamic-form-multiple") dynamicForms: QueryList<DynamicFormMultiple>

  getAllFormsData() {
    this.dynamicForms.forEach(formComponent => {
      console.log(formComponent.getFormAsJson());
    })
  }
}
于 2019-07-06T12:49:58.980 回答