【问题标题】:How to get total sum of a column in ng-repeat如何在 ng-repeat 中获取列的总和
【发布时间】:2020-04-14 06:59:38
【问题描述】:

我正在尝试获取 My ng-repeat 表中列的总和。我在本段下方分享了表格的图像。

表格代码如下。

<table>
   <tr>
     <th>Ref No</th><th>Model</th><th>IMEI</th><th>Color</th><th>Warranty</th><th>Branch</th><th>Party</th><th>Date of Sell</th><th>Amount</th>
   </tr>
  <tr ng-repeat="x in Approve"><td> {{ x.sno }} </td><td> {{ x.p_model }} </td><td> {{ x.imei }} </td><td> {{ x.color }} </td><td> {{ x.warrenty }} </td><td> {{ x.branch }} </td><td> {{ x.party }} </td><td> {{ x.dsell }} </td><td ng-init="Approve.total.amount = Approve.total.amount + x.rate"> {{ x.amount | number:2 }} </td></tr>
  <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td style="color:red;">Grand Total</td><td>{{ grandtotal }}</td></tr>
</table>

我的控制器代码如下。

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("api/sold_out.php").then(function (response) {
$scope.Approve = response.data.message;
});
});
</script>

Jsone 从服务器获取:

{"message":[{"0":"2","sno":"2","1":"Huawei P9 Lite ","p_model":"Huawei P9 Lite ","2":"545454545454","imei":"545454545454","3":"White","color":"White","4":"CGC","warrenty":"CGC","5":"50","amount":"50","6":"1","sell":"1","7":"27\/02\/2017","dsell":"27\/02\/2017","8":"1","transfer":"1","9":"MANSOURA","branch":"MANSOURA","10":"Cloud International Co Will","party":"Cloud International Co Will"},{"0":"3","sno":"3","1":"Samsung Galaxy J7 6","p_model":"Samsung Galaxy J7 6","2":"456545454545545","imei":"456545454545545","3":"Gold","color":"Gold","4":"Gasham","warrenty":"Gasham","5":"100","amount":"100","6":"1","sell":"1","7":"27\/02\/2017","dsell":"27\/02\/2017","8":"1","transfer":"1","9":"MANSOURA","branch":"MANSOURA","10":"Cloud International Co Will","party":"Cloud International Co Will"}]}

我需要在总计区域中显示 Amount 列的总 sum 值。

【问题讨论】:

  • 如何在控制器中创建一个函数,将列表中的这些值相加,并在 html 中引用该函数?
  • 我知道这不是您要的,但是在您的视图中包含这种计算逻辑并不是最佳实践。我建议将此字段推送到模型中,或者在您的控制器中计算然后推送到模型中。它还允许您编写有关如何计算的单元测试。
  • 告诉我步骤。
  • 按照@seva 所说的,您还可以实现一个过滤器来显示您的总计,例如&lt;td style="color:red;"&gt;Grand Total&lt;/td&gt;&lt;td&gt;{{ Approve | sumColumn: 'amount' }}&lt;/td&gt;。如果您对此感兴趣,我将用示例将其写为答案
  • 你能用我的代码给我看一个完整的例子吗?我像你说的那样更新了代码。创建了一个过滤器,但它使整个东西消失了。

标签: angularjs


【解决方案1】:

正如我在评论中所说,解决此问题的一种方法是使用如下所示的自定义过滤器:

(function() {
  'use strict';

  angular
    .module('Test', [])
    .filter('sumByColumn', function () {
      return function (collection, column) {
        var total = 0;

        collection.forEach(function (item) {
          total += parseInt(item[column]);
        });

        return total;
      };
    });
})();

然后,您可以通过指定要对哪一列求和来调用集合中的过滤器:

<td>{{ Approve | sumByColumn: 'amount' }}</td>

恕我直言,在这里使用过滤器的优势在于它是可重复使用的。每当您需要对任何数据的特定列求和时,您都可以再次使用此过滤器。

当然我建议你通过检查列值是否是一个数字来改进它,例如......等等......因为它是一个过滤器,你可以很容易地为它编写测试。

我创建了一个 JsFiddle 来演示它在您的上下文中是如何工作的 https://jsfiddle.net/7b3q2q5p/4/

【讨论】:

  • 我在问题中添加了控制器代码。你能用过滤器更新控制器代码吗?我添加了过滤器代码,但没有显示总值。
  • 好吧,因为我无法访问您的上下文(也就是您的服务器响应),所以我无法真正粘贴您的控制器并使其工作。我认为这是不起作用,因为您从服务器获取数据的方式(这可能是一种解释)。我认为在这里你应该使用一些依赖注入以避免将这种逻辑放在你的控制器中。如果您使用的是Router,我建议您使用它的resolve 方法并从此处注入服务器响应。它不仅使它更易于测试,而且还使它更不容易出错。
  • 使用过滤器后,我得到了这样的结果05001000 500 和 1000 都转换为字符串并放在一起。
  • 我用 json 结果更新了代码。代码在哪里获取结果。
  • 然后使用parseInt 将您的字符串值转换为整数。我更新了我的 jsFiddle 和我的示例
【解决方案2】:
<table>
   <tr>
     <th>Ref No</th><th>Model</th><th>IMEI</th><th>Color</th><th>Warranty</th><th>Branch</th><th>Party</th><th>Date of Sell</th><th>Amount</th>
   </tr>
  <tr ng-repeat="x in Approve"><td> {{ x.sno }} </td><td> {{ x.p_model }} </td><td> {{ x.imei }} </td><td> {{ x.color }} </td><td> {{ x.warrenty }} </td><td> {{ x.branch }} </td><td> {{ x.party }} </td><td> {{ x.dsell }} </td><td ng-init="$parent.grandtotal  = grandtotal + x.rate"> {{ x.amount | number:2 }} </td></tr>
  <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td style="color:red;">Grand Total</td><td>{{ grandtotal }}</td></tr>
</table> 

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2017-05-30
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多