首页 文章

如何处理响应形式的对象数组中的响应

提问于
浏览
2

我得到一个对象数组作为响应,并将其分配给具有formControlName'custom'的多选(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

我是角度反应形式的初学者,我对如何使用formArray和formGroup感到困惑 . 以及如何处理对象数组的响应 .

希望您理解我的问题,如果您需要任何清晰请注释 .

提前致谢 .

1 回答

  • 2

    试试这样:

    DEMO

    访问表单数组元素:

    {{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
          })])
        });
      }
    }
    

相关问题