Maureen Moore

Maureen Moore

  • NA
  • 206
  • 0

Grand Totals Not Updating Upon Delete

Aug 2 2020 5:45 PM
I am using a getter to update the grand totals. When I click on the deleteItem button, the grand total isn't updated and the table cell displays the same grand total as before even though items have been removed. How can I fix this?
In the HTML:
  1. <form name="yourForm" [formGroup]="yourForm">  
  2. <table>  
  3. <tr *ngFor="let item of items; let i = index">  
  4. <td><td></td>  
  5. <button type="button" (click)="deleteItem(i)" class="deletebtn">X</button></td>  
  6. <td>{{item.quantity}} </td>  
  7. <td>{{item.product_price }} </td>  
  8. <td>{{item.quantity * item.product_price }}</td>  
  9. </tr>  
  10. <tr><td></td><td></td><td><b>Grand Total</b> </td><td><b>{{grandTotal }}</b></td></tr>  
  11. </table>  
In the component:
  1. totals = [];  
  2. get grandTotal() {  
  3.   
  4. let i;  
  5. let sub_total = 0;  
  6. let grand_total = 0;  
  7. if (this.isSubmitted == true) {  
  8. if (typeof this.product_price !== "undefined" && typeof this.quantity !== "undefined") {  
  9. sub_total = this.product_price * this.quantity;  
  10. this.totals.push(sub_total);  
  11. }  
  12. }  
  13. for (i = 0; i < this.totals.length; i++) {  
  14. grand_total += this.totals[i];  
  15. }  
  16. this.isSubmitted = false;  
  17. return grand_total;  
  18. }  
  19.   
  20.   
  21.   
  22. deleteItem(i){  
  23. this.totals = [];  
  24. this.grandTotal;  
  25. this.items.splice(i,1);  
  26. this.setStorageItems(this.items);  
  27. this.isSubmitted = true;  
  28. }  

Answers (3)