首页 > 解决方案 > 填充表单数组

问题描述

晚安,我在填充表单数组时遇到以下问题,我设法将代码开发到了一个步骤,但现在我陷入了困境。我设法使用 setControl 填充数组,但现在我需要在这个数组中填充一个数组,我遇到了很多麻烦。我能够填充原料数组,但无法填充 feedstockOptions 数组按照以下代码:

Method populate form

if (response.feedstocks?.length >=1 && response.feedstocks != null) {
  this.feedstockForm.setControl('feedstock', this.editFeedstock(response.feedstocks))              
}
  editFeedstock(response) {
    const formArray = new FormArray ([]);
    response.forEach (s => {
    formArray.push ( this.fb.group ({
      category: s.category,
      position: s.position,
      feedstockOptions: s.feedstockOptions (//this.editFormOptions(s.feedstockOptions))
    }));
  });
  return formArray;
  }
  
    editFormOptions(response) {
    const array = new FormArray([]);
    response.forEach(r => {
      array.push(this.fb.group({
        feedstockOptions: r.feedstockOptions
      }))
    })
    console.log(response)
    return array
    
  }
Form
  feedstockForm: FormGroup = this.fb.group({
    feedstock: this.fb.array([
      this.feedstockFormsCategory()
    ])
  });
  feedstockFormsCategory(): FormGroup {
    return this.fb.group({
      category:  "",
      position: "",
      feedstockOptions: this.fb.array([
        this.feedstockFormsComponent()
      ])
   })
  };
  feedstockFormsComponent(): FormGroup {
    return this.fb.group({
     name: '',
     code: '',
     price:  this.fb.group({
       amount: ['', Validators.required],
       currency: ['BRL'],
     }),
   })
  };

标签: angulartypescriptformsangular-reactive-formsformarray

解决方案


罗德,您可以使用“地图”而不是 forEach 并推送。“map”数组函数将一个数组转换为另一个数组。

例如

//Is the same
const formArray = new FormArray ([]);
response.forEach (s => {
  formArray.push (
    this.fb.group ({
       category: s.category,
       position: s.position)
    });
return formArray;

//that
return response.map(s=>{
    return this.fb.group ({
       category: s.category,
       position: s.position,)
    });
})

看到我们用数组的值转换了 FormGroup 数组中的对象数组

好吧,当您创建函数来创建表单时,您可以将对象作为“可选”参数传递。所以你的函数 feedstockFormsCategory 和 feedstockFormsComponent 变得像。

   feedstockFormsCategory(data:any=null): FormGroup {
    data=data || {category:'',position:'',feedstockOptions:null}
    return this.fb.group({
      category:data.category,
      position: data.position,
      feedstockOptions: data.feedstockOptions?this.fb.array(
                               data.feedstockOptions.map(x=>this.feedstockFormsComponent(x))):
                      this.fb.array([]) //a empty array
                                        //you can also return an array with one element
                                        //if you use
                                        //this.fb.array([this.feedstockFormsComponent()])
      ])
   })
  };

  feedstockFormsComponent(data:any=null): FormGroup {
    data=data|| {name:'',code:'',price:{amount:'',currency:'BRL'}}
    return this.fb.group({
     name: data.name,
     code: data.code,
     price:  this.fb.group({
       amount: [data.price.amount, Validators.required],
       currency: [data.price.currency],
     }),
   })
  };

看到您可以使用响应的值创建一个 FormArray

 this.formArray=this.fb.array(response.map(x=>this.feedstockFormsCategory(x))

推荐阅读