Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate Totals Of FormArray Angular

I wanted to multiply the num1 to num2 and put it to the subtotal. and if I push another row it will also multiply the num1 to num2

Lastly I want to add all the subtotal and put it in total how to achieve that?

here is my stackblitz that i created

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://stackblitz.com/edit/mat-dialog-example-scrwra

>Solution :

You can do this by using angular form value changes like this:

this.Info.valueChanges.subscribe((datas: Array<any>) => {
      var total = 0;
      datas.forEach((data, index) => {
        const sub = data.num1 * data.num2;
        total = total + sub;
        this.Info.controls[index]
          .get('subtotal')
          .patchValue(sub, { emitEvent: false });
      });
      this.fg.get('total').patchValue(total);
    });

https://stackblitz.com/edit/mat-dialog-example-qw3kpz?file=app%2Fapp.component.ts,app%2Fapp.component.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading