0

I am selecting two checkboxes and the amount is getting calculated in Total Claimed in the 1st div but 2 nd div also the amount is getting calculated. The reason I know; Its coz the div is created using array and I have one common variable. Now the issue arises how to create a dynamic variable ?

enter image description here

In below code, you will find {{claimed}} which is the common variable and I need this to be dynamic:

<p>Total Claimed: [<span [ngClass]="{'text-success': claimed <= d.program_max_limit, 'text-warning': claimed > d.program_max_limit}">{{claimed | number : '2.'}}&nbsp;</span> Of {{d.program_max_limit | number : '2.'}}]</p>

component.ts This is the function of the calculation portion:

  calc(event,val, id) {
    console.log(val,id)
    var i=0;
    if(id == event.target.value){
       if(event.target.checked ){ 
        this.claimed = parseInt(this.claimed) + parseInt(val);
        if (this.claimed > this.max_amount) {
          this.push.showError("You Have Reached the Maximum Limit");
          this.isDisabled = true;
          this.checkForm.reset();
          this.claimed = 0;
        } else {
          this.isDisabled = false;
        }
      } else {
        this.claimed = parseInt(this.claimed) - parseInt(val);
      }

    }
2
  • is this still pending? Commented Apr 6, 2020 at 4:16
  • @GouravGarg no. the answer is given by Eliseo, But anyway thanx Commented Apr 6, 2020 at 9:07

1 Answer 1

1

define a variable type array of boolean and use in the table.

check:boolean[]=[]; //<--see that you equal to an array

I suppose you has some like

<tr *ngFor="let item of listItems;let i=index">
<td>{{item.invoiceAmmount}}</td>
...
<td><input type="checkbox" [(ngModel)]="check[i]"></td>
</tr>

Then you can use a getter to total

get total()
{
   let total=0;
   this.listItems.forEach((x,index)=>{
     if (check[index])
         total+=x.invoiceAmmount;
   })
   return total;
}

And in your .html

Total:{{total}}
<div *ngIf="total>max_ammount">You has execeded tha ammout!</div>

NOTE: If you dont want use a getter you can use

<td><input type="checkbox" [ngModel]="check[i]"
                           (ngModelChange)="check[i]=$event;getTotal()">
</td>

getTotal()  //<--your function getTotal
{
       let total=0;
       this.listItems.forEach((x,index)=>{
         if (check[index])
             total+=x.invoiceAmmount;
       })
       this.total=total //<--store the total in a variable "total"
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. I have applied it, but if user check[0], it is getting checked in two boxes simultaneously
the first row is check[0], the second one check[1], the trhird check[2], see that use ;let i=index and [ngModel]="check[i]"
I agree but there is no 3rd checkbox, wait i 'll show you my schema. i'll edit my answer
sure, you has so many "checks" as number of rows has your table
I have edited the answers and thnx for your efforts. I really appreciate it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.