简体   繁体   English

从网格列 Ionic 4 获取总值

[英]Get total value from grid column Ionic 4

Can someone help me to solve this issue.有人可以帮我解决这个问题。 I want to get the total value of the last column value.我想得到最后一列值的总值。 below is my HTML code.下面是我的 HTML 代码。

  <div *ngFor="let order of orders |filter:searchText">
  <ion-row>
    <ion-col>
      <ion-text>
        {{order.OrderNo}}
      </ion-text>
    </ion-col>
    <ion-col>
      <ion-text>
        {{order.CustomerName}}
      </ion-text>
    </ion-col>
    <ion-col>
      <ion-text>
        {{order.PaymentType}}
      </ion-text>
    </ion-col>
    <ion-col>
      <ion-text>
        {{order.TotalPrice | currency:''}}
      </ion-text>
    </ion-col>
  </ion-row>
</div>
<h6>Total value: </h6>

我的网格

You can just use javascript in your template like:您可以在模板中使用 javascript,例如:

<h6>Total value: {{ orders.reduce((acc, nxt) => acc + nxt) | currency:'' }}</h6>

However, having logic in your template is generally bad practice.但是,在模板中包含逻辑通常是不好的做法。 Create a function in your component and call it from your template: example.component.ts在您的组件中创建一个 function 并从您的模板中调用它:example.component.ts

// replace any with actual type
sum(orders: any[]): number {
   return orders.reduce((acc, nxt) => acc + nxt);
}

example.component.html example.component.html

<h6>Total value: {{ sum(orders) | currency:'' }}</h6>

try using pipe尝试使用 pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'total'
})
export class TotalPipe implements PipeTransform {
  transform(array: any): any {
    let total = 0;
    array.forEach(element => {
        total += element.totalPrice;
        // you may need to modify total price if it contains any currency and convert it into int
    });

    return total;
 }
}

And use it into html file like并将其用于 html 文件,如

<h6>Total value: {{ orders | total }}</h6>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM