使用* ngFor并从中提交值的动态表单

时间:2018-12-10 15:13:27

标签: html angular typescript angular-material

我有 stackblitz 作为指南。

我想显示一个物料卡列表,单击“编辑”按钮,可以在其中编辑文本字段,当我单击“保存”图标时,它当然会通过触发功能等进行保存

但是,我正在努力掌握这一切在Angular中的工作方式以及我的应用程序的Material本质。

html

<form id="myForm" [formGroup]="thisIsMyForm">
  <mat-card [formGroup]="x" *ngFor="let x of data; let i = index">
    <mat-form-field>
      <label for="{{x.name}}">Name</label>
      <input formControlName="name" id="{{x.name}}" matInput value="{{x.name}}">
    </mat-form-field>
    <mat-form-field>
      <label for="{{x.type}}">Type</label>
      <input formControlName="type" id="{{x.type}}" matInput value="{{x.type}}"/>
    </mat-form-field>
  </mat-card>
</form>

ts

import { Component, ViewChild } from '@angular/core';
import {MatSnackBar} from '@angular/material';
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  thisIsMyForm: FormGroup

  data = [
    {name:"one", type:"one"},
    {name:"two", type:"two"},
    {name:"three", type:"three"},
  ];

  constructor(private formBuilder: FormBuilder) {}

  onSubmit() {
    // Here I would like to be able to access the values of the 'forms'
  }

}

2 个答案:

答案 0 :(得分:4)

您肯定正在深入研究,尝试在*ngFor范围内构建动态的反应形式是一个挑战。我将尽你所能。

您将需要为控件创建一个数组,在构造函数中,使用formArrayName将表单设置this.formBuild.array([])创建为一个空数组...随便叫什么,我只是用{{1 }}用于演示。

使用空数组调用formArrayName实例化表单后

this.buildForm()

在您的constructor(private formBuilder: FormBuilder) { this.thisIsMyForm = new FormGroup({ formArrayName: this.formBuilder.array([]) }) this.buildForm(); } 中,对buildForm()进行迭代,并为每个索引推送控件,同时分配默认值和默认状态Disabled。

data[]

请注意::console.log(controlArray.controls)产生以下输出...每个索引都是一个具有两个控件 buildForm() { const controlArray = this.thisIsMyForm.get('formArrayName') as FormArray; Object.keys(this.data).forEach((i) => { controlArray.push( this.formBuilder.group({ name: new FormControl({ value: this.data[i].name, disabled: true }), type: new FormControl({ value: this.data[i].type, disabled: true }) }) ) }) console.log(controlArray) } name的FormGroup

type

在您的html中,您需要建立一个模仿您刚创建的0: FormGroup 1: FormGroup 2: FormGroup 的容器层次结构。

  • 父母:thisIsMyForm
  • 孩子:thisIsMyForm
  • 孙子:formArrayName

孙子很重要,因为它与上一步中的i as formGroupName的控制台日志匹配

controlArray.controls

根据控件的禁用状态创建编辑和保存按钮

<form id="myForm" [formGroup]="thisIsMyForm">
    <div [formArrayName]="'formArrayName'">
        <mat-card *ngFor="let x of data; let i = index">
            <div [formGroupName]="i">

在组件中创建方法以接收索引作为参数,并处理逻辑以隐藏按钮并切换输入字段的启用和禁用状态。

   <button *ngIf="fomrControlState(i)" (click)="toggleEdit(i)">Enable Edit</button>
    <button *ngIf="!fomrControlState(i)" (click)="toggleEdit(i)">Save</button>

提交按钮,单击该按钮可单击console.log的表单值...当任何输入formControl处于启用状态时,也将禁用按钮。

toggleEdit(i) {
    const controlArray = this.thisIsMyForm.get('formArrayName') as FormArray;
    if(controlArray.controls[i].status === 'DISABLED'){
      controlArray.controls[i].enable()
    }else{
      controlArray.controls[i].disable()
    }
  }

  fomrControlState(i){
     const controlArray = this.thisIsMyForm.get('formArrayName') as FormArray;
     return controlArray.controls[i].disabled
  }

Stackblitz

https://stackblitz.com/edit/dynamic-form-ngfor-otbuzn?embed=1&file=src/app/app.component.ts

答案 1 :(得分:0)

使用QueryList进行操作:

您的html(这是一个示例):

<ng-container *ngFor="let x of data; let i = index">
  <div class="ctr">
    <span #names class="item">{{x.name}}</span>
    <span #types class="item">{{x.type}}</span>
    <span class="button" (click)="showData(i)">Show data</span>
  </div>

</ng-container>

<h2>Selected values: </h2>
Selected name: {{selectedName}} <br>
Selected type: {{selectedType}}

一些仅用于样式的css

.ctr{
  display: flex;
  flex-direction: row;
  margin-bottom: 20px;
}

.item{
  margin-right:40px;
}

.button{
  border: 1px solid black;
  padding: 2px 5px 2px 5px;
  cursor: pointer;
}

组件:

import { Component, QueryList, ViewChildren } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  @ViewChildren('names') names:QueryList<any>;
  @ViewChildren('types') types:QueryList<any>;

  selectedName: string;
  selectedType: string;

  data = [
    {name:"one", type:1},
    {name:"two", type:2},
    {name:"three", type:3},
  ];

  showData(index){
    let namesArray = this.names.toArray();
    let typesArray = this.types.toArray();

    this.selectedName = namesArray[index].nativeElement.innerHTML;
    this.selectedType = typesArray[index].nativeElement.innerHTML;
  }
}

有效的堆叠闪电战:https://stackblitz.com/edit/angular-j2n398?file=src%2Fapp%2Fapp.component.ts