【问题标题】:Add a row to table on click of add button the new row is form fields in each cell单击添加按钮向表格添加一行,新行是每个单元格中的表单字段
【发布时间】:2019-02-08 00:55:27
【问题描述】:

实际上,在添加单击时,我需要添加行,但我无法获取 ngmodel 对象或者是否有任何其他使用反应式表单实现的最佳方法,所以最后的要求是在添加单击时获取一行并获取表单值最好的实现方式或者请修改上面的代码

Stackblitz Link

【问题讨论】:

    标签: angular angular-reactive-forms angular-forms


    【解决方案1】:

    在此处查看现场演示。

    http://keepnote.cc/code/form-group-with-formarray-adding-dyamic-row-angular-5-6

    请检查一下。

    对于动态行,我们必须使用 FormBuilderFormBuilder.array

    html

    <form [formGroup]="carForm" (ngSubmit)="saveIntergration()">
        <div formArrayName="details" class="form-group" *ngFor="let field of carForm.get('details').controls; let ind = index;">
            <div [formGroupName]="ind">
                Type:
                <input type="text" formControlName="type">
    
                model:
                <input type="text" formControlName="model">
    
                year:
                <input type="text" formControlName="year">
    
                make:
                <input type="text" formControlName="make">
    
                color
                <input type="text" formControlName="color">
    
                plateNumber
                <input type="text" formControlName="plateNumber">
    
                <hr>
            </div>
        </div>
    </form>
    
    <button (click)="addRow()">Add New</button>
    
    <pre>
        {{carForm.value | json}}
    </pre>
    

    ts

    import { Component, ViewChild } from '@angular/core';
    import { FormBuilder, FormGroup, FormControl, Validators, FormArray } from '@angular/forms';
    
    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
    
    })
    export class AppComponent {
        public carForm: FormGroup;
        constructor(private fb: FormBuilder) {
    
            const items = [];
            items.push(this.fb.group({
                type: [],
                model: [],
                year: [],
                make: [],
                color: [],
                plateNumber: []
            }));
    
            this.carForm = this.fb.group({
                details: this.fb.array( items )
            });
        }
    
        addRow() {
            const details = this.carForm.get('details') as FormArray;
            details.push(this.createItem());
        }
    
        createItem(): FormGroup {
            return this.fb.group({
                type: [],
                model: [],
                year: [],
                make: [],
                color: [],
                plateNumber: []
            });
        }
    }
    

    【讨论】:

    • 谢谢兄弟,它也有效,我的工作也完成了如何使表单仅在添加点击时才显示我得到一行是否可能?
    • 酷!请接受这个答案。在构造函数中加载了一项。
    • 加载一项意味着
    • 详细解释一下。使表单最初仅在添加单击时显示我得到一行是否可能
    • 所以在添加表单之前初始化可以在添加点击后初始化
    【解决方案2】:

    Angular 中的反应式表单:使用FormArray 动态创建表单字段。

    请根据给定的示例更改您的代码。

    HTML:

    <form [formGroup]="orderForm" (submit)="onSubmit()">
      <div class="row">
        <button type="button" (click)="addItem()">add</button></div>
      <div *ngFor="let item of data" class="row">
        <div class="col">{{item.Type}}</div>
        <div class="col">{{item.Model}}</div>
        <div class="col">{{item.Year}}</div>
        <div class="col">{{item.Make}}</div>
        <div class="col">{{item.Color}}</div>
        <div class="col">{{item.PlateNumber}}</div>
        <div class="col">{{item.StateRegistered}}</div>
      </div>
      <div formArrayName="items" *ngFor="let item of orderForm.get('items').controls; let i = index;" class="row">
        <div [formGroupName]="i">
          <input formControlName="Type">
          <input formControlName="Model">
          <input formControlName="Year">
          <input formControlName="Make">
          <input formControlName="Color">
          <input formControlName="PlateNumber">
          <input formControlName="StateRegistered">
        </div>
      </div>
      <div class="row">
        <button type="submit">Submit</button>
      </div>
    </form>
    

    TS:

    import { Component, OnInit } from '@angular/core';
    import { FormGroup, FormArray, FormBuilder } from '@angular/forms';
    
    @Component({
      selector: 'app-stack53513269',
      templateUrl: './stack53513269.component.html',
      styleUrls: ['./stack53513269.component.css']
    })
    export class Stack53513269Component implements OnInit {
      orderForm: FormGroup;
      items: FormArray;
      data: any = [];
      constructor(private formBuilder: FormBuilder) {
        this.data.push({ Type: 'SUV', Model: '500', Year: '2009', Make:'Mahinda',   Color:'Red',    PlateNumber:'RJ-15',    StateRegistered:'Rajsthan' });
      }
    
      ngOnInit() {
        this.orderForm = this.formBuilder.group({
          items: this.formBuilder.array([this.createItem()])
        });
      }
    
      createItem(): FormGroup {
        return this.formBuilder.group({
          Type:'',  Model:'',   Year:'',    Make:'',    Color:'',   PlateNumber:'', StateRegistered:''
        });
      }
    
      addItem(): void {
        this.items = this.orderForm.get('items') as FormArray;
        this.items.push(this.createItem());
      }
    
      onSubmit() {
        this.items = this.orderForm.get('items') as FormArray;
        console.log(this.items.value);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 2019-01-20
      相关资源
      最近更新 更多