I've got a form that has the ability to add/remove rows of items. Whenever I want to go to edit my form, I run an asynchronous call to grab some data then patch/set the values of the form controls accordingly. Everything works as expected, however, with my rows of items, when I utilize Angular 2's patchValue
function, it only patches a form group with the first object of an array of objects, instead of all of the objects.
So for example, if my form had 3 rows of line items, like in an invoice, only the first one is being populated. Can anyone tell me what I'm doing wrong?
Here is my component functionality
constructor() {
this.createCheckRequestForm();
}
createCheckRequestForm() {
this.createRequestForm = this._fb.group({
issued_date: [new Date().toISOString(), [<any>Validators.required]],
due_date: [new Date().toISOString(), [<any>Validators.required]],
invoice_number: ['', [<any>Validators.required]],
how_to_send: ['Mail', [<any>Validators.required]],
normal_processing: ['', []],
is_corporation: ['', []],
line_items: this._fb.array([this.initLineItem()])
});
}
/**
* Init the line item for the form builder
*/
initLineItem(options?) {
return this._fb.group({
description: ['', []],
coding: ['', []],
amount: ['', []],
line_item_types: [[], []]
});
}
populateFormWithRequest(requestId) {
this.formPopulationObs = this._checkRequests.getRequest(requestId).subscribe(requestData => {
// Iterate through the line items and patch them
let lineItems = [];
requestData.line_items.forEach((lineItem, index) => {
let lineItemTypes = [];
if(lineItem.line_item_types) {
lineItemTypes = lineItem.line_item_types;
}
// Separate key-store for the coding dropdown models
this.lineItemCodings[index] = lineItem.coding;
let lineItemObj = {
description: lineItem.description,
coding: lineItem.coding,
amount: lineItem.amount,
line_item_types: lineItemTypes,
}
lineItems.push(lineItemObj);
})
this.createRequestForm.patchValue({line_items: lineItems});
})
}
If I have an array like this, only the first element shows up in the "patch"
[
{amount: "600", coding: "-Kfxw732pfWUS5rU0TRu", description: "Cameras"},
{amount: "600", coding: "-Kfxw732pfWUS5rU0TRu", description: "Lights"}
]
Thanks in advance!
line_items
that don't exist in the form yet. By default, the form creates only 1line_item
and if I try to patch 3line_items
, it only gets the first one, since only one has been instantiated, I believe. - Stevie Star