简体   繁体   English

将动态值添加到 Angular 4 中表内的输入字段

[英]Add Dynamic value to input field inside a table in Angular 4

I am trying to implement a dynamic row table where all the table cells are input fields.我正在尝试实现一个动态行表,其中所有表格单元格都是输入字段。 There is some preloaded data, but I am not able to add to the loaded data in the view.有一些预加载的数据,但我无法添加到视图中加载的数据。 However add/ delete rows are working properly.但是添加/删除行工作正常。 I have tried ngModel and value but it's not working.我已经尝试过 ngModel 和 value 但它不起作用。

app.component.html: app.component.html:

    <table>
            <thead>
                <button (click)="onAddRow()" *ngIf="addForm.get('rows')">add row</button>
            </thead>
            <tbody>
                <tr *ngFor="let row of addForm.get('rows')?.controls;let index = index;">
          <!-- NOT WORKING with ngModel
          <td>name : <input [ngModel]="row.get('name')" [formControl]="row.get('name')"></td> -->
            <!-- NOT WORKING with value attribute
          <td>name : <input value="row.get('name')" [formControl]="row.get('name')">\</td> -->
          <td>
            name : <input  [formControl]="row.get('name')">
          </td>
          <td>
            description : <input [formControl]="row.get('description')">
          </td>
          <td>
            qty : <input [formControl]="row.get('qty')">
          </td>
          <td>
            <button (click)="onRemoveRow(index)">Remove</button>
          </td>
                </tr>
            </tbody>
        </table>

app.component.ts: app.component.ts:

import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl, FormArray, NgForm } from '@angular/forms'

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

  constructor(private fb: FormBuilder) {
    this.addForm = this.fb.group({
      items: [null, Validators.required],
      items_value: ['no', Validators.required]
    });
    this.rows = this.fb.array([{
    "name": "Test Item",
    "description": "Dedsc",
    "qty": "q23"
  }]);

  }

  ngOnInit() {
    this.addForm.get("items").valueChanges.subscribe(val => {
      if (val === true) {
        this.addForm.get("items_value").setValue("yes");

        this.addForm.addControl('rows', this.rows);
      }
      if (val === false) {
        this.addForm.get("items_value").setValue("no");
        this.addForm.removeControl('rows');
      }
    });
  }

  onAddRow() {
    this.rows.push(this.createItemFormGroup());
  }

  onRemoveRow(rowIndex:number){
    this.rows.removeAt(rowIndex);
  }

  createItemFormGroup(): FormGroup {
    return this.fb.group({
      name: null,
      description: null,
      qty: null
    });
  }

}

My output:我的 output: 电流输出

Expected output:预期 output: 预期产出

Also putting one Stackblitz Example for better reference.还放了一个Stackblitz 示例以供更好的参考。

You don't need [ngModel] because you already working with reactive forms.您不需要[ngModel] ,因为您已经在使用响应式 forms。 These inputs are empty because you create each item form group with null values.这些输入为空,因为您使用null值创建每个项目表单组。 If you have preloaded data you need to pass it as initial value for each control in createItemFormGroup method or update the value by methods setValue/patchValue of form array or appropriate form group.如果您已预加载数据,则需要将其作为createItemFormGroup方法中每个控件的初始值传递,或通过表单数组或适当表单组的setValue/patchValue方法更新值。

i am not sure if that will be exactly what you want, but check this one out: https://stackblitz.com/edit/dynamically-add-rows-k2lbkw我不确定这是否正是您想要的,但请检查一下: https://stackblitz.com/edit/dynamically-add-rows-k2lbkw

  • i replaced the formControl attribute with formControlName.我用 formControlName 替换了 formControl 属性。 Note the value property注意value属性
<td>
     name : <input  formControlName="name" 
                   [ngModel]="row.get('name').value" >
</td>
  • added the name, description, qty to the form builder object.将名称、描述、数量添加到表单构建器 object。
    constructor(private fb: FormBuilder) {
        this.addForm = this.fb.group({
          items: [null, Validators.required],
          items_value: ['no', Validators.required],
          name: [null, Validators.required],
          description: [null, Validators.required],
          qty: [null, Validators.required]
        });
        this.rows = this.fb.array([]);
      }
  • and added some dummy values on the createItemFormGroup method并在 createItemFormGroup 方法上添加了一些虚拟值

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

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