The solution was to make a pipe on the ngfor
import { Pipe, PipeTransform } from '@angular/core';
import { InvoiceService } from 'app/components/application/invoices/invoice.service';
@Pipe({
name: 'invoiceFilteretList'
})
export class InvoiceFilteretList implements PipeTransform {
constructor( private invoiceService: InvoiceService ) {}
transform(value: any, q: string, type) {
let totalSum = 0;
let remaining = 0;
for (let i = 0; i < value.length; i ++) {
totalSum += value[i].totalPrice;
if ( value.hasOwnProperty( 'paid' )) {
if (!value.paid) { // if not paid
remaining += value.totalPrice;
}
}
}
this.invoiceService.SetTotalSum( totalSum );
this.invoiceService.SetRemainingSum( remaining );
return value;
}
}
then I made a service for storing the sum and remaning sum
import { ReplaySubject } from 'rxjs/ReplaySubject';
export class InvoiceService {
public totalSum = new ReplaySubject<any>();
public remainingSum = new ReplaySubject<any>();
SetTotalSum( value: number ) {
this.totalSum.next( value );
}
SetRemainingSum( value: number ) {
this.remainingSum.next( value );
}
}
and last I made a method for getting the updated sums
filteredInvoiceList() {
this.invoiceService.totalSum
.subscribe(res => {
this.filteredSum = res;
});
this.invoiceService.remainingSum
.subscribe(res => {
this.filteredSumNotPaid = res;
});
}
and display those in the view