Reputation: 464
I am creating rows in a table using ng-repeat. I need to add variables to them self to get a running total. However, once that variable is larger than 25 I need to reset it back to 0. Here is my current code:
<tr ng-repeat="payoutqueue in payoutqueues" ng-init="payoutqueue.totalValue = +payoutqueues[$index-1].totalValue + +payoutqueue.balance_plain">
<td align="left">{{$index}}</div></td>
<td align="right"><div>{{payoutqueue.address}}</div></td>
<td align="right"><div>{{payoutqueue.balance}}</div></td>
</tr>
I want to add a div behind <div>{{payoutqueue.balance}}</div>
like this:
<div>{{payoutqueue.balance}}</div><div ng-if="payoutqueue.totalValue > 25">END</div>
and I only want <div ng-if="payoutqueue.totalValue > 25">END</div>
to show up once payoutqueue.totalValue
is greater than 25. I got this to work using ng-if
, however, once I show <div ng-if="payoutqueue.totalValue > 25">END</div>
I also need to reset payoutqueue.totalValue=0
so I can start adding the loop again.
I tried doing this with help from another answer doing this: <div ng-if="payoutqueue.totalValue > 25">END{{payoutqueue.balance=0;""}}</div>
however this didn't work.
To sum it up, I want to add a <div>
after every time a variable added to itself is greater than 25. After the variable is greater than 25 I need to reset it to 0 and start adding to it again.
Here is what I am looking for if I were to do this in PHP:
<?php
$totalValue=0;
foreach($payoutqueues as $payoutqueue){
$totalValue=$totalValue+payoutqueue['balance_plain'];
echo $totalValue;
if($totalValue > 25){
$totalValue=0;
}
}
?>
UPDATE So I have not really found a solution that works well for me, so I just decided to do the math on the server side and add an extra element to my JSON file. Thank you to everyone who tried to help.
Upvotes: 1
Views: 1202
Reputation: 464
So I have not really found a solution that works well for me, so I just decided to do the math on the server side and add an extra element to my JSON file. Thank you to everyone who tried to help.
Upvotes: 0
Reputation: 10555
You could perhaps do something hack-ish like:
<div ng-if="payoutqueue.totalValue > 25 && !(payoutqueue.balance = 0)">END</div>
But really you should move the logic into the controller, for example you could build a dictionary with the totals and access it via $index
Upvotes: 0
Reputation: 662
Hello try using the following code
<tr ng-repeat="payoutqueue in payoutqueues" ng-init="checkTotalValue(payoutqueue)">
<td align="left">{{$index}}</div></td>
<td align="right"><div>{{payoutqueue.address}}</div></td>
<td align="right">
<div>{{payoutqueue.balance}}</div>
<div ng-if="payoutqueue.totalValue > 25">
END
</div>
</td>
</tr>
In Controller:
$scope.totalValue = 0;
$scope.checkTotalValue = function(payoutqueue){
$scope.totalValue = $scope.totalValue + payoutqueue.balance_plain;
payoutqueue.totalValue = $scope.totalValue;
if($scope.totalValue > 25)
{
$scope.totalValue = 0;
}
}
This will work. Following is the jsfiddle for the same: Demo
Upvotes: 1