【问题标题】:Iterating through Angular Reactive Form遍历 Angular 响应式表单
【发布时间】:2020-05-02 19:42:22
【问题描述】:

我有一个动态创建的 Angular 响应式表单。

我有一个函数,当“速率”或“数量”字段发生变化时会调用该函数。我想遍历表单并将每个项目的费率和数量相乘,然后将它们加在一起以获得总计。

我该怎么做?我试过使用 .forEach,但这似乎不适用于 FormArrays

这是我表单的 json 结构:

{
  "quoteDate": "2019-01-11T11:00:00",
  "quoteItems": [
    {
      "description": "",
      "quoteLineItems": [
        {
          "rate": 8,
          "quantity": 78,
        },
        {
          "rate": 2,
          "quantity": 32,
        }
      ]
    },
    {
      "description": "",
      "quoteLineItems": [
        {
          "rate": 24,
          "quantity": 5,
        }
      ]
    },
    {
      "description": "",
      "quoteLineItems": [
        {
          "rate": 1,
          "quantity": 2145,
        }
      ]
    }
  ]
}  

这就是我创建表单的方式:

createForm() {
    this.quoteForm = this.fb.group({
      quoteDate: [this.quote.quoteDate, Validators.required],
      quoteItems: this.fb.array([])
    });
    this.addQuoteV2Item();
}

get quoteItems() {
  return this.quoteForm.get('quoteItems') as FormArray;
}

quoteLineItems(index) {
  return (this.quoteForm.get('quoteItems') as FormArray).at(index).get('quoteLineItems') as FormArray

}

addQuoteItem() {
  this.quoteItems.push(this.fb.group({
    description: '',
    quoteLineItems: this.fb.array([])
  }));
}

addQuoteLineItem(index, description) {
  this.quoteLineItems(index).push(
    this.fb.group({
      rate: '',
      quantity: '',
      total: 0
    })

  )
}

【问题讨论】:

    标签: angular


    【解决方案1】:

    这就是我最终解决的方法:

    updateTotal() {
      var subtotal = 0;
      var numberOfQuoteItems = (this.quoteForm.get('quoteItems') as FormArray).length;
    
      for (let i = 0; i < numberOfQuoteItems; i++) {
          var numberOfQuoteLineItems = ((this.quoteForm.get('quoteItems') as FormArray).at(i).get('quoteLineItems') as FormArray).length;
          for (let j = 0; j < numberOfQuoteLineItems; j++) {
            var currentLineItem = ((this.quoteForm.get('quoteItems') as FormArray).at(i).get('quoteLineItems') as FormArray).at(j);
            subtotal = subtotal + currentLineItem.value.rate * currentLineItem.value.quantity;
          }
      }
      this.subtotal = subtotal;
    
    }
    

    【讨论】:

      【解决方案2】:

      你需要的是 valueChanges 方法,它返回一个 observable,所以你需要订阅它。

      ngOnInit() {
        this.quoteForm.valueChanges.subscribe(inputField => {
          if (inputField.rate) {
            // Do something with the rate value
          } else if (inputField.quantity) {
            // Do something with the quantity value
          }
        });
      }
      

      【讨论】:

      • 如何获得嵌套的数量和费率字段?
      • @RobbieMills 如果我没记错的话,this.quoteForm.quoteDate.quoteItems[0]quoteLineItems[0]rate 返回8。如果您想要动态值,则需要遍历每个数组(在我的情况下,我刚刚使用[0] 作为第一个值)。现在您可以使用forforEach 来执行此操作。
      • 好的,问题是我不能在 FormArray 上使用 forEach(不是函数错误),这就是我需要做的
      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 2020-05-19
      • 2018-05-05
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多