Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取以角度表示的列的总和_Javascript_Angularjs_Calculator_Totals - Fatal编程技术网

Javascript 获取以角度表示的列的总和

Javascript 获取以角度表示的列的总和,javascript,angularjs,calculator,totals,Javascript,Angularjs,Calculator,Totals,我很难得到计算出的总数。我需要做什么才能得到第三列的总数 <body ng-app="Test"> <section style="margin-top:80px"> <div ng-controller="TestController as test" > <table class="table"> <tr> <th>Name</th>

我很难得到计算出的总数。我需要做什么才能得到第三列的总数

<body ng-app="Test">
  <section style="margin-top:80px">
    <div ng-controller="TestController as test" >
      <table class="table">
        <tr>
          <th>Name</th>
          <th>Number</th>
          <th>Amount</th>
          <th>Reduced Amount</th>
        </tr>

        <tr ng-repeat="x in test.approve">
          <td> {{ x.name }} </td>
          <td> <input class="qty form-control" type="number" ng-model="x.number" ng-change="sumByColumn3()" min="0" restrict-to="[0-9]"/> </td>
          <td> <span ng-model="x.amount" ng-change="sumByColumn()">{{ x.number*x.amount }}</span> </td>
          <td> <input class="qty form-control" type="number" ng-model="x.reducedAmount" ng-change="sumByColumn2()" min="0" restrict-to="[0-9]"/> </td>
        </tr>
        <tr>
          <td style="color:red;">Totals go here</td>
          <td>{{ test.approve | sumByColumn3: 'number' }}</td>
          <td>{{ test.approve | sumByColumn: 'amount' }}</td>
          <td>{{ test.approve | sumByColumn2: 'reducedAmount' }}</td>
        </tr>
      </table>

    </div>
  </section>


</body>

名称
数
数量
减少的金额
{{x.name}
{x.number*x.amount}
总数在这里
{{test.approve}sumByColumn3:'number'}
{{test.approve | sumByColumn:'amount'}
{{test.approve}sumByColumn2:'reducedAmount'}

这是完整的代码

在html的第三列中,您有:

<td> <span ng-model="x.amount" ng-change="sumByColumn()">{{ x.number*x.amount }}</span> </td>
但这里你错过了:

.filter('sumByColumn', function () {
      return function (collection, column) {
        var total = 0;

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

        return total;
      };
    })
我想如果你把乘法加起来,你会得到正确的总数:

parseInt(item[column]*item.number);
parseInt(item[column]*item.number);