2017-12-24 10 views
0

Cloud FireStore의 새로운 기능으로 Observable에서 오는 배열의 일부 필드 값을 합산하는 방법에 대한 간단한 해결책을 찾기 위해 며칠 동안 결과를 검색했습니다. Angular Firestore. 내 템플릿에서 Observable의 필드 값을 각도 4 Firestore 배열로 집계하는 방법

constructor(private afs: AngularFirestore, private router: Router, private route: ActivatedRoute) { 
    this.negocio = this.route.snapshot.queryParams['negocioId'] 
    this.movtosCollectionRef = this.afs.collection('movtos', ref => ref.where('empresa', '==', this.negocio)); 


    this.movtos$ = this.movtosCollectionRef.snapshotChanges().map(actions => { 
     return actions.map(action => { 
      const data = action.payload.doc.data() as Movtos; 
      const id = action.payload.doc.id; 
      return { id, ...data }; 
     }); 


    }); 
    } 

내가 찾아합니다 : I 행의 목록을 가지고 있고이 합계 원하는 열 합계가 여기 내 코드의 일부입니다

 <tbody> 
      <tr *ngFor="let item of movtos$ | async" [class.completed]="item.completed"> 
       <td>{{ item.indate | date: 'dd/MM/yyyy - HH:mm:ss'}}</td> 
       <td>{{ item.concepto }} </td> 
       <td>{{ item.tipomov }} </td> 
       <td>{{ item.inuser }} </td> 
       <td>{{ item.importe | currency:'USD':true:'3.2-2' }}</td> 
       <td> 
        <a class="btn btn-info" (click)="editaEmpresa(item.id)" tooltip="Edita Movimiento" placement="top"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a> 

       </td> 

      </tr> 

     </tbody> 

을 그리고 싶어 item.importe 필드의 합계. 신체가 이것을하는 방법을 알고 있습니까? 감사합니다. Caribesoft

답변

0

Observable에서 map을 사용하면 합계를 반환 할 수 있습니다. 예 :

getSum(): Observable<number> { 
    return this.observable 
     .map(arr => arr.reduce((a, b) => a + b.value, 0)); 
} 
+0

응답에 대해 Rahul에게 감사하지만 "this.observable"은 어디에서 얻을 수 있습니까? 나는 this.movtos $로 시도했지만 작동하지 않습니다. – CaribeSoft

+0

당신은 약속대로 응답을 받고 있습니까? 그런 다음 당신은'Observed.fromPromise()'를 사용할 수 있습니다. –

+0

나는 이것을 시도하고 있습니다 -> getSum() : Observable { this.movtos $ 을 반환합니다. .map (arr => arr.reduce ((a, b) => a + b.importe, 0)); }하지만 얻을 수 있습니다. TOTAL : [object Object] – CaribeSoft