如何处理以角形的反应形式排列的对象阵列的响应

时间:2018-08-19 16:13:13

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

我得到一个对象数组作为响应,并将其分配给具有formControlName'custom'的multiselect(ng-select)。我得到的回复看起来像这样

this.taskList = [
    { 'clientTaskId' : '1', 'taskName': 'hardware setup', billableRate: '500', customRate: ''},
    { 'clientTaskId' : '2', 'taskName': 'software installation', billableRate: '250', customRate: ''},
    { 'clientTaskId' : '3', 'taskName': 'environment setup', billableRate: '700', customRate: ''},
    { 'clientTaskId' : '4', 'taskName': 'cafeteria setup', billableRate: '1200', customRate: ''},
    ];

这是我的formGroup

 clientForm = this.fb.group([
 custom: ['']
 ])

现在无论我选择什么对象。我将其填充在一个表中,该表的一列是可编辑的。现在,我在表格的可编辑列中编辑数据,当我点击保存按钮时,应该会收到包含已编辑数据的响应。

我已经使用formControl名称填充了数据。

这是我的代码:

import {Component, NgModule, ViewChild} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormControl, FormGroup, ReactiveFormsModule, FormsModule, FormBuilder,Validators, FormArray} from '@angular/forms';
import {NgSelectModule, NgOption} from '@ng-select/ng-select';

@Component({
    selector: 'my-app',
    template: `
    <form [formGroup]="clientForm" (ngSubmit)="submit(clientForm.value)">
      <ng-select 
        placeholder="Select custom rates"
        [items]="taskList"
        [multiple]="true"
        bindLabel="taskName"
        [addTag]="true"
        [closeOnSelect]="false"
        clearAllText="Clear"
        formControlName = "custom"
        >
      </ng-select>

      <br>
      <table class="table">
        <thead class="thead-light">
          <tr>
          <th scope="col">Task Name</th>
            <th scope="col">Custom Rate</th>
            <th scope="col">Standard Rate</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let task of clientForm.controls['custom'].value">
            <td>{{ task.taskName }}</td>
            <td>
              <input class="form-control form-control-sm" type="text" placeholder="" >
            </td> 
            <td>{{ task.billableRate}}</td>
          </tr>
        </tbody>
      </table>
      <br>
    <button type="submit">Submit</button>
    </form>
    <br>
    `
})
export class AppComponent {
  taskList : any;

  ngOnInit() {
    this.taskList = [
    { 'clientTaskId' : '1', 'taskName': 'hardware setup', billableRate: '500', customRate: ''},
    { 'clientTaskId' : '2', 'taskName': 'software installation', billableRate: '250', customRate: ''},
    { 'clientTaskId' : '3', 'taskName': 'environment setup', billableRate: '700', customRate: ''},
    { 'clientTaskId' : '4', 'taskName': 'cafeteria setup', billableRate: '1200', customRate: ''},
    ];
  }

  submit(formValue){
    console.log(formValue)
  }
  clientForm = this.fb.group({
    custom : ['']
  })
  constructor(private fb: FormBuilder ) {  }

}

这是演示: demo

我是angular-active-forms的初学者,对于如何使用formArray和formGroup感到有些困惑。以及如何处理对象数组的响应。

希望您能理解我的问题,如果需要任何说明,请发表评论。

谢谢。

1 个答案:

答案 0 :(得分:2)

尝试这样:

DEMO

以以下形式访问表单Array元素:

{{clientForm.controls.customer.controls[i].controls.billableRate.value}}

代码HTML和TS:

import { Component, NgModule, ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormControl, FormGroup, ReactiveFormsModule, FormsModule, FormBuilder, Validators, FormArray } from '@angular/forms';
import { NgSelectModule, NgOption } from '@ng-select/ng-select';

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="clientForm" (ngSubmit)="submit(clientForm.value)">

      <table class="table" formArrayName="customer">
        <thead class="thead-light">
          <tr>
          <th scope="col">Task Name</th>
            <th scope="col">Custom Rate</th>
            <th scope="col">Standard Rate</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let task of clientForm.controls['customer'].controls; let i= index" [formGroupName]="i" >
            <td>{{clientForm.controls.customer.controls[i].controls.taskName.value}}</td>
            <td>
              <input formControlName="customRate" class="form-control form-control-sm" type="text" placeholder="" >
            </td> 
            <td>{{clientForm.controls.customer.controls[i].controls.billableRate.value}}</td>
          </tr>
        </tbody>
      </table>
      <br>
    <button type="submit">Submit</button>
    </form>
    <br>

    <div>{{clientForm.value |json}}</div>
    `
})
export class AppComponent {
  customer: FormArray;
  clientForm: FormGroup;
  taskList: Array<any> = [
    { clientTaskId: '1', taskName: 'hardware setup', billableRate: '500', customRate: '' },
    { clientTaskId: '2', taskName: 'software installation', billableRate: '250', customRate: '' },
    { clientTaskId: '3', taskName: 'environment setup', billableRate: '700', customRate: '' },
    { clientTaskId: '4', taskName: 'cafeteria setup', billableRate: '1200', customRate: '' },
  ];

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.createForm();

    this.customer = this.getData();

    while (this.getData().length) {
      this.customer.removeAt(0)
    }

    for (let d of this.taskList) {
      this.addMore(d);
    }
  }

  getData() {
    return this.clientForm.get('customer') as FormArray;
  }

  addMore(d) {
    this.getData().push(this.buildCusforms(d));
  }

  buildCusforms(data) {
    if (!data) {
      data = {
        clientTaskId: null,
        taskName: null,
        billableRate: null,
        customRate: null
      }
    }
    return this.fb.group({
      clientTaskId: data.clientTaskId,
      taskName: data.taskName,
      billableRate: data.billableRate,
      customRate: data.customRate
    });
  }


  createForm() {
    this.clientForm = this.fb.group({
      customer: this.fb.array([this.buildCusforms({
        clientTaskId: null,
        taskName: null,
        billableRate: null,
        customRate: null
      })])
    });
  }
}