首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理来自angular中以反应形式出现的对象数组的响应

如何处理来自angular中以反应形式出现的对象数组的响应
EN

Stack Overflow用户
提问于 2018-08-20 00:13:13
回答 1查看 1.7K关注 0票数 2

我正在获取一个对象数组作为响应,并将其分配给一个具有formControlName‘formControlName’的多选(ng-select)。我得到的响应如下所示

代码语言:javascript
复制
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

代码语言:javascript
复制
 clientForm = this.fb.group([
 custom: ['']
 ])

现在不管我选择什么对象。我将其填充到一个表中,其中表的一列是可编辑的。现在,我编辑表的可编辑列中的数据,当我单击保存按钮时,应该会得到包含编辑后的数据的响应。

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

下面是我的代码:

代码语言:javascript
复制
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-reactive-forms的初学者,我对如何使用formArray和formGroup感到有点困惑。以及如何处理对象数组的响应。

希望你能理解我的问题,如果你需要澄清,请发表意见。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-08-20 01:13:09

试着这样做:

DEMO

访问表单数组元素如下所示:

代码语言:javascript
复制
{{clientForm.controls.customer.controls[i].controls.billableRate.value}}

代码HTML和TS:

代码语言:javascript
复制
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
      })])
    });
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51919558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档