Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

Calculate sub total of specific item in angular

How can I apply the sub total amount on specific item.

So far, I have these sample on stackblitz: When I add new item and change the quantity of that last item all items I added recently were updating as well. I would like to apply the update the "get subTotal() changeSubtotal()" to a specific item only.

Here' my Component.

//Component

items = this.cartService.getItems();
subTotal: any;

 //-----  total 
    get total(){
        return this.items.reduce((sum,x)=>  ({
            qtyTotal:1,
            variationCost:sum.variationCost+x.qtyTotal*x.variationCost
        }),
        {qtyTotal:1,variationCost:0}).variationCost;
    }

    //-----  subtotal
    // get subTotal(){
    //     return this.items.reduce((_sum,x)=>  ({
    //         qtyTotal:1,
    //         variationCost:x.qtyTotal*x.variationCost
    //     }),
    //     {qtyTotal:1,variationCost:0}).variationCost;
    // }

    changeSubtotal(index: number){
    console.log(index)
       return this.subTotal = this.items.reduce((sum, item) => sum += 
        (item.qtyTotal || 0) * (item.variationCost || 0) ,0)
     }

Here's my HTML

// HTML

<div *ngFor="let addedItem of items; let i = index ">
   <div>
     <input id="{{ addedItem.variationId }}"
     [(ngModel)]="addedItem.qtyTotal"
     [ngModelOptions]="{standalone: true}" min="1" type="number"  
     value="{{ addedItem.qtyTotal }}" 
     (click)="changeSubtotal(i)">
    </div>
    <div>{{ subTotal | currency }}</div>
</div>

<div>{{ total | currency }}</div>

Sample code here: stackblitz

question from:https://stackoverflow.com/questions/65902955/calculate-sub-total-of-specific-item-in-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I fixed it.

I referenced the element by using @ViewChildren decorator and created a method to target the specific item.

//HTML
<div *ngFor="let addedItem of items; let i = index " >
   <div>
     <input id="{{ addedItem.variationId }}"
     [(ngModel)]="addedItem.qtyTotal"
     [ngModelOptions]="{standalone: true}" min="1" type="number"  
     value="{{ addedItem.qtyTotal }}" 
     (change)="changeSubtotal($event.target.value, addedItem.variationCost,i)">
    </div>
    <div #subTotalWrap>{{ addedItem.variationCost | currency }}</div>
</div>

<div>{{ total | currency }}</div>

...

//Component
@ViewChildren('subTotalWrap') subTotalItems: QueryList<ElementRef>;

changeSubtotal(qty, amt, index) {
   const subTotal = amt * qty;
   this.subTotalItems.toArray()[index].nativeElement.innerHTML = subTotal
}

https://stackblitz.com/edit/angular-ivy-i8kpub?file=src/app/app.component.ts


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...