如何在 Angular JS 中的 ng-repeat 中创建小计

How to create subtotals in a ng-repeat in Angular JS

我刚刚开始学习 AngularJS 并且需要创建下面的报告。我的所有明细行都运行良好,但不知道如何创建小计行。

详细行...

<tr data-ng-repeat-start="t in testReferrers">
    <td>{{t.ReferrerName}}</td>
    <td>{{t.AddressLine1}}}</td>
    <td>{{t.DatePlaced  | date:'MM/dd/yyyy'}}</td>
    <td>{{t.InvoiceNumber }}</td>
    <td>{{t.InvoiceAmountLessDiscount | currency : $ : 2 }}</td>
</tr>

我第一次尝试小计线,但我不知道如何计算 {{subTotal}} 以及如何控制此行何时出现。我需要分组和分组页脚功能,但不知道如何在 AngularJS 中执行此操作。我打算使用 JQuery 找到 subTotalRow 并显示或隐藏...

<tr id="subtotalRow" data-ng-repeat-end style="display:none">
    <td colspan=3></td>
    <td style="border-top: solid 1px #000000">Total:</td>
    <td style="border-top: solid 1px #000000">{{subTotal | currency : $ : 2 }}</td>
</tr>

期望的输出...

angular.module('app', []).controller('ctrl', function($scope){
  $scope.data = [
    {Referrer: 'Henry', Amount: 20, Location: 'NY'},
    {Referrer: 'Tom', Amount: 10, Location: 'London'},    
    {Referrer: 'Sam', Amount: 10, Location: 'Paris'},
    {Referrer: 'Henry', Amount: 10, Location: 'NY'},
    {Referrer: 'Tom', Amount: 20, Location: 'London'},    
    {Referrer: 'Henry', Amount: 30, Location: 'NY'}
  ];
  $scope.sum = function(name){
    return $scope.data.filter(function(x) { return x.Referrer == name; })
      .map(function(x) { return x.Amount; }).reduce(function(a, b) { return a + b; });
  }
})
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
.totalRow{
  border-style: solid;  
}
.total{  
  text-align: right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js">
</script>

<div ng-app='app' ng-controller='ctrl'>
  <table>
    <thead>
      <tr>
        <th>Referrer</th>
        <th>Location</th>        
        <th>Amount</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-init='next = $index + 1' ng-repeat-start='item in dataSorted = (data | orderBy : "Referrer")'>
        <td>{{item.Referrer}}</td>
        <td>{{item.Location}}</td>
        <td>{{item.Amount}}</td>
      </tr>
      <tr class='totalRow' ng-repeat-end ng-if='!dataSorted[next] || (dataSorted[next].Referrer != item.Referrer)'>
        <td colspan='2' class='total'>Total:</td>        
        <td>{{sum(item.Referrer)}}</td>
      </tr>
    </tbody>
  </table>
</div>