Angular / Form / Multi Rows or Items in a form
Multi Rows or Items in a form
Read Basic Form topics before reading this topics
-
In controller
Step1: import FormBuilder, FormGroup , FormControl in controller
import { FormBuilder, FormGroup, Validators, FormArray, FormControl, NgForm } from '@angular/forms'; Step2: declare FormGroup & orderItems variables
orderForm: FormGroup; orderItems: any; Step 3: injust FormBuilder into constructor()
constructor(private fb: FormBuilder){ } Step4: define orderForm
this.orderForm = this.fb.group({ 'id': [''], 'order_date': [''], 'customer_id': [''], 'orderItems': this.fb.array([ ]), }); Step5: define orderItems
createItemFormGroup(): FormGroup { return this.fb.group({ orderId: [''], itemCode: [''], itemName: [''], price: [''] }); } Step6: add the multiple row to the orderForm
onAddRow() { this.orderItems = this.orderForm.get('orderItems') as FormArray; this.orderItems.push(this.createItemFormGroup({})); } 1) createItemFormGroup() is defined in Step5
2) onAddRow() is called from html(View)Step7: remove item
onRemoveRow(rowIndex: number) { this.orderItems.removeAt(rowIndex); } onRemoveRow() is called from html -
In View page
Step1: add form tag
Step2: add form elements
Add form elements and link with formGroup
Code
Step 3: add multiple row elements
1) looping the orderItems : *ngFor="let row of orderForm.get('orderItems')?.controls;let index = index;"
2) link formElements in the loop : [formControl]="row.get('itemCode')"Code
Step 4: add button to append new row
Step5 : add button to delete a row
Delete -
Additional Features
set/get multi-row variables based on index in components
//set ((this.itemForm.get('addItemsRequests') as FormArray).at(index) as FormGroup).get('itemCode').patchValue('123'); //get var code = this.itemForm.get('addItemsRequests')?.value[rowIndex].itemCode; Loop the multi-rows in component (set / get in the loop)
let orderItems=((this.itemForm.get('addItemsRequests') as FormArray)); let sum=0; orderItems.controls.forEach(x => { // get var itemtotal=x.get('total').value; sum=sum+parseFloat(itemtotal); this.orderForm.get('grandTotal').patchValue(sum); //set x.patchValue( {total:120} ); }); Pre-populate data into multi-row form
FormGroupthis.orderForm = this.formBuilder.group({ sohdrId: [0], orgId: ['', Validators.required], customerId: ['', Validators.required], salesOrderDtls: this.formBuilder.array([ ]) });
Loop the API datacreateItem(item:any): FormGroup { return this.formBuilder.group({ itemId: item.itemid, orgId:this.orgId, itemCode:[item.itemCode, Validators.required], itemName:[item.itemName], quantity: [item.qtyRequired, Validators.required], expectedDate: [item.expectedDate], basePrice:item.basePrice, total:[item.total, Validators.required], uomName:item.uomName, minOrderQty:item.minOrderQty, }); } //pass empty object for new entry addItem(): void { this.items = this.orderForm.get('salesOrderDtls') as FormArray; this.items.push(this.createItem({})); } this.orderService.getDetails(this.orderId,this.orgId) .subscribe(data => { //start loop data.salesOrderitems.forEach(row => { //get formArray this.items = this.orderForm.get('salesOrderDtls') as FormArray; this.items.push(this.createItem( { itemid:row.itemid, itemCode:row.itemCode, itemName: row.itemName, basePrice:row.basePrice, uomName:row.uomName, minOrderQty:row.minOrderQty, expectedDate:expectedDate, qtyRequired:row.qtyRequired, total:total } )); }); }); Clear FormArray item
(this.orderForm.controls['salesOrderDtls'] as FormArray).clear();