gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:29:07 25 4
gpt4 key购买 nike

我有一个 stackblitz 作为指南。

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

然而,我正在努力掌握这一切在 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'
}

}

最佳答案

您肯定正在深入研究,试图在 *ngFor 的范围内构建一个动态的 react 形式。是一个挑战。我会尽我所能引导你完成它。

您将需要为控件创建一个数组,在您的构造函数中创建您的表单设置 formArrayName作为使用 this.formBuild.array([]) 的空数组...随便你怎么称呼它,我刚用过 formArrayName用于演示目的。

使用空数组实例化表单后,调用 this.buildForm()

constructor(private formBuilder: FormBuilder) {
this.thisIsMyForm = new FormGroup({
formArrayName: this.formBuilder.array([])
})

this.buildForm();
}

在您的 buildForm()迭代您的 data[]并为每个索引推送控件,同时分配默认值和默认状态禁用。
 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)
}

请注意: console.log(controlArray.controls) 产生以下输出...每个索引是一个 FormGroup,带有两个控件 nametype
0: FormGroup
1: FormGroup
2: FormGroup

在您的 html 中,您需要建立一个模仿 thisIsMyForm 的容器层次结构。你刚刚创建。
  • 家长:thisIsMyForm
  • child :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="formControlState(i)" (click)="toggleEdit(i)">Enable Edit</button>
    <button *ngIf="!formControlState(i)" (click)="toggleEdit(i)">Save</button>

    在组件中创建方法以接收索引作为参数并处理逻辑以隐藏按钮和切换输入字段启用和禁用状态。
    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()
    }
    }

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

    单击提交时,console.log 的表单值的提交按钮...当任何输入 formControls 处于启用状态时也禁用按钮。
    <button [disabled]="thisIsMyForm.get('formArrayName').enabled" (click)="onSubmit()">Submit Form</button>

    onSubmit() {
    // Here I would like to be able to access the values of the 'forms'
    console.log(this.thisIsMyForm.value)
    }

    Blitz

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

    关于html - 使用 *ngFor 的动态表单并从中提交值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53708499/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com