有什么办法可以使第3列与最后一列的总和有所不同?

<body ng-app="Test">
  <section style="margin-top:80px">

    <h3>Plastic Calculator Form Version 2.0</h3>

    <div ng-controller="TestController as test" >
      <p>To date,
        <strong><u># of people who pledged</u></strong>
        Earthlings have pledged to reduce
           their single-use plastic waste from
          <strong><u>{{ test.approve | sumByColumn: 'amount' }}</u>
          </strong> Items per year to <strong>
          <u>{{ test.approve | sumByColumn5: 'amount' }}</u>
        </strong>.
        That's a reduction of
        <strong><u>{{ test.approve | sumByColumn4: 'reducedTotal' }}</u>
        </strong> per year!  Do your part.  Make a pledge!
      </p>




      <table class="table">
        <tr>
          <th>Single-Use Plastic Items</th>
          <th>Enter the Number You Use Per Week</th>
          <th>The Number You Use Per Year is:</th>
          <th>How Many Less Can You Use Per Week?</th>
          <th>Your Reduced Usage Per Year Would Be:</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 ng-model="x.amount"> {{ x.number*x.amount }} </td>
          <td> <input class="qty form-control" type="number"
                      ng-model="x.reducedAmount" ng-change="sumByColumn2()"
                      min="0" restrict-to="[0-9]"/>
          </td>
          <td ng-model="x.reducedTotal"> {{ x.reducedAmount*x.reducedTotal }}
          </td>
        </tr>
        <tr>
          <td>TOTALS</td>
          <td>{{ test.approve | sumByColumn3: 'number' }}</td>
          <td>{{ test.approve | sumByColumn: 'amount' }}</td>
          <td>{{ test.approve | sumByColumn2: 'reducedAmount' }}</td>
          <td>{{ test.approve | sumByColumn4: 'reducedTotal' }}</td>
        </tr>
      </table>




      <form>
        <div class="col-sm-4">
          <div class="form-group">
            <label for="full-name">Name</label>
            <input type="text" class="form-control" id="full-name"
                   placeholder="Enter Full Name">
          </div>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <label for="email-address">Email</label>
            <input type="email" class="form-control" id="email-address"
                   aria-describedby="emailHelp" placeholder="Enter email">
          </div>
        </div>
        <div class="col-sm-4">
          <button type="submit" class="btn btn-primary" style="margin-top:25px">
            Submit
          </button>
        </div>
      </form>
    </div>
  </section>

</body>



此外,当用户点击“提交”时,有没有办法让表格在第3列中累计总数?换句话说,在其中说:

single-use plastic waste from
<strong>
  <u>{{ test.approve | sumByColumn: 'amount' }}</u>
</strong> Items per year


每次用户填写表格时都会将总数加起来。

这是完整的代码-https://codepen.io/tampham/pen/RzqLQV

至于获得差异,这是我到目前为止尝试过的。

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

      collection.forEach(function (item) {
        total += (item.amount-item.reducedTotal);
      });

      return total;
    };
})


任何建议都会有很大帮助,谢谢!

最佳答案

您已经具有以下几行来计算第3列和第5列的总计

{{(test.approve | sumByColumn: 'amount') }}
{{(test.approve | sumByColumn4: 'reducedTotal')}}


我认为对您来说,最简单的解决方案是使用以下方法在两者之间取得区别。

{{(test.approve | sumByColumn: 'amount') - (test.approve | sumByColumn4: 'reducedTotal')}}


Demo

07-26 03:35