Calculate and display total of input field when changing in Angular material table

I have a angular material table with input fields in it. Need to calculate the total (without button) and display at the bottom. Also when change the input number , should recalculate and display. It calculates and displays sum of what is in the ts file but when changes in the input box, concatenate . HTML code

<ng-container matColumnDef="tuesday">
  <th mat-header-cell *matHeaderCellDef> tuesday </th>
  <td mat-cell *matCellDef="let transaction">
    <mat-form-field>
      <input  [(ngModel)]="transaction.tuesday" placeholder="Field Name" matInput ="number"/>
         </td>
  <td mat-footer-cell *matFooterCellDef> {{getTotalCostTuesday() | currency}} </td>
</ng-container>

TS code

  transactions: PeriodicElement[] = [
  { project: 'Hydrogen', monday:1, tuesday:2},
  { project: 'Helium',  monday:1 ,tuesday: 5 },
  { project: 'Lithium' , monday:1, tuesday: 3},
  { project: 'Beryllium' , monday:1, tuesday: 4 },

  ];
getTotalCostTuesday() {
  return this.transactions.map(t => t.tuesday
    ).reduce((acc, value) => acc + value, 0);
   
}


Read more here: https://stackoverflow.com/questions/66384694/calculate-and-display-total-of-input-field-when-changing-in-angular-material-tab

Content Attribution

This content was originally published by Sz2013 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: