Is there a method to calculate the variance between column 3 and the last one in a table?
<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 committed to reducing
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>
Moreover, is it possible to update the total in column 3 of the form when the user clicks submit? Essentially, I want the sentence:
single-use plastic waste from
<strong>
<u>{{ test.approve | sumByColumn: 'amount' }}</u>
</strong> Items per year
to increment with each form entry by the user.
The complete code can be found here - https://codepen.io/tampham/pen/RzqLQV
In terms of calculating the difference, this is what I've attempted so far.
filter('sumByColumn5', function () {
return function (collection, column) {
var total = 0;
collection.forEach(function (item) {
total += (item.amount-item.reducedTotal);
});
return total;
};
})
If you have any recommendations, I would greatly appreciate it. Thank you!