Calculate in ng-repeat from the entry before - angularjs

I have a filtered list (which is filtered by time - so in a specific timeframe) and over these item I am iterating with ng-repeat. These items have a name and a price. So if I am iterating over them I want to achieve that I always show the "sub"-total like this:
DATE NAME PRICE SUBTOTAL
2014-05 T-Shirt 20.00 20.00
2014-05 Jeans 45.00 65.00
2014-05 Cap 15.00 80.00
These Items are sorted by date but might have a different ID (ids dont match the index!).
I am really not able to find out how I could always calculate the subtotal (the table can be filtered by date ranges, means I could also include the items from 2014-04 and it should recalculate dynamically.
I tried it with a function like this in the controller:
var curBalanceCounter2 = 0;
$scope.currentBalanceCalc = function(finance) {
curBalanceCounter2 = curBalanceCounter2 + finance.amount;
return curBalanceCounter2;
}
But this i being executed 10 times so I get wrong numbers. Any better solution?
Thank you.

Create a custom filter
myApp.filter('subtotal', function(){
return function(items, index){
var subtotal = 0;
for (var i = 0; i <= index; i++) {
subtotal += items[i].price
}
return subtotal || items[index].price;
}
});
and call it like so
<li ng-repeat="item in items">{{item.name}} - {{item.price}} -
{{ items | subtotal : $index}}</li>
Demo
Since you have access to the original list (e.g. items in the code above) inside of an ng-repeat, you can pass it, along with the index of the current item, into a custom filter. This filter can then loop through each item up to and including the index passed in, and then return a summed subtotal. If the subtotal is 0 (as it would be for a first item), instead return the price of that item.
Docs: Custom filters in Angular

This is similar to Marc's answer. Define a subtotal function in the controller:
$scope.subtotal = function(index){
var total = 0;
angular.forEach($scope.data, function(value, key){
if(key <= index)
total += value.Price;
});
return total;
}
Then use it like this in the view:
<tr ng-repeat="d in data">
<td>{{d.Date}}</td>
<td>{{d.Name}}</td>
<td>{{d.Price}}</td>
<td>{{subtotal($index)}}</td>
</tr>
Demo
Update
If the issue is that the data isn't already sorted on the client, but is being sorted by a filter on the ng-repeat, then here's the fix:
Pass in the orderBy parameter to the subtotal function, and execute the filter on the data before computing the subtotals:
$scope.orderBy = 'Date';
$scope.subtotal = function(index, orderBy){
var total = 0;
angular.forEach($filter('orderBy')($scope.data,orderBy), function(value, key){
if(key <= index)
total += value.Price;
});
return total;
}
I've updated my demo with this code. You can change the sort order by changing 'Date' to 'Name' or 'Price' on this line
$scope.orderBy = 'Date';
and see that the subtotals automatically recalculate.

I don't know of a way to do this in pure angular, perhaps someone will chime in.
What you need looks like a cumulative sum:
function cSum(arr) {
var cumsum = [];
for(var i=0;i<arr.length;i++) {
if(i==0) cumsum[i] = arr[0];
else cumsum[i] = cumsum[i-1] + arr[i];
}
return cumsum
}
Then just add that field into the array of objects that you are repeating over and you can display it in the table.

Not too hard to do http://jsfiddle.net/VAJ5S/3/
HTML
<div ng-app="myApp">
<table ng-controller="myController">
<thead>
<tr>
<th>DATE</th>
<th>NAME</th>
<th>PRICE</th>
<th>SUBTOTAL</th>
</tr>
</thead>
<tr ng-repeat="item in items">
<td>{{item.date}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>{{subtotal($index)}}</td>
</tr>
</table>
</div>
JS
var app = angular.module("myApp", []);
app.controller("myController", ["$scope", function($scope){
$scope.items = [
{
date: "2014-05",
name: "T-Shirt",
price: 20.00
},
{
date: "2014-05",
name: "Jeans",
price: 65.00
},
{
date: "2014-05",
name: "Cap",
price: 80.00
}
];
$scope.subtotal = function(ind){
var subtotal = 0;
for (var i = 0; i<=ind; i++){
subtotal += $scope.items[i].price;
}
return subtotal;
};
}]);

Related

How can I dynamically limit data showing in an ng-repeat?

I have this ng-repeat that I show split over three rows here for clarity:
ng-repeat="row in phs.phrasesView =
(phs.phrases | orderBy:phs.phrasesOrderBy[phs.phrasesOrderById]
.key:phs.phrasesSortDirectionId == 1)">
On my form I have a field phs.keywordRange
Is there a way that I can make it so the rows returned are filtered as follows:
When phs.keywordRange is null or empty string, all rows are shown
When phs.keywordRange is A then only rows where row.keyword starts with A are shown
When phs.keywordRange is ABC then only rows where row.keyword starts with ABC are shown
Make your own filter, like this fiddle.
Changing the $scope.keywordRange will update the list accordingly.
as shortcut:
.filter('keywordRange', function() {
return function(value, keyword) {
var out = [];
if(!keyword) {
return value;
}
for(var i = 0; i < value.length; i++) {
if(value[i].startsWith(keyword)){
out.push(value[i]);
}
}
return out;
};
});
function MyCtrl($scope) {
$scope.keywordRange = 'ti';
$scope.lines = [
'rest1', 'rest2', 'tiago', 'pedro', 'america'
];
}
and the html
<div ng-controller="MyCtrl">
<div ng-repeat="line in lines | keywordRange:keywordRange">
<p>{{line}}</p>
</div>
</div>

How to calculate the sum of repeated elements in AngularJS?

I am new to AngularJS and stuck on a problem from last one week.
I am getting dynamic weekly values for different groups.
My target is to caculate the sum of values for each group which is doable through filter, but I am not able to figure out how to re-calulate the weeklyvalues if I change the value of sum. What I have done so far:
Controller:
app.controller("CalculationController", [
"$scope", "calculationService", function ($scope, calculationService) {
getGroups();
function getGroups() {
calculationService.GetGroups().success(function(groups) {
$scope.groups= groups;
console.log(groups);
}).error(function (error) {
$scope.status = "Error Occured: " + error.message;
console.log($scope.status);
});
}$scope.getTotal = function () {
var groupTotal = [];
var total = 0; // hold the total for week
var totalPerGroup = 0;
var totalPerGroupArr = [[], []]; //array to store total values Per Group
var groups = $scope.groups; //getting the data of groups in a variable.
var totalCostForEachWeekPerMainGroup = [[], []]; //array to store total groups for each week
var totalGroupsCostPerGroup = []; //array to store the total cost per group
totalCostPerMainGroup = 0; //sum of all groups per Main Group
for (var i = 0; i < trps.length; i++) {
total = 0;
totalPerGroup = 0;
for (var j = 0; j < groups[i].WeeklyValues.length; j++) {
totalCostForEachWeekPerMainGroup[j][i] = groups[j].WeeklyValues[i].Cost;
totalPerGroupArr[i][j] = groups[i].WeeklyValues[j].Cost;
totalPerGroup += totalPerGroupArr[i][j];
total += totalCostForEachWeekPerMainGroup[j][i];
totalCostPerMainGroup += totalCostForEachWeekPerMainGroup[j][i];
}
groupTotal.push(total);
totalGroupsCostPerGroup.push(totalPerGroup);
$scope.totalGroupsCostPerMainGroup = totalCostPerMainGroup;
}
//Percentage calculation for each week
return groupTotal;
}
}]).filter('sumCalculation', function () {
return function (data, key) {
if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
return 0;
}
var total = 0;
for (var i = 0; i < data.length; i++) {
total += parseFloat(data[i][key]);
}
//$scope.sum = total;
return total;
}});
View:
<table class="table table-hover">
<tr>
<th>#Html.Label("Main Group")</th>
<th>#Html.Label("Group")</th>
<th ng-repeat="w in numberOfWeeks" ng-cloak>W{{w}}-2016</th>
<th>#Html.Label("Total")</th>
</tr>
<tr ng-repeat="(k, v) in GroupTotalPerMainGroupPerWeek"> // coming from another service
<td>{{k}}</td>
<td></td>
<td></td>
<td ng-repeat="cost in getTotal()">
{{cost}}
</td>
<td>{{totalGroupsCostPerMainGroup}}</td>
</tr>
<tr ng-repeat="g in groups">
<td></td>
<td ng-cloak>{{g.GroupName}}</td>
<td ng-repeat="c in g.WeeklyValues">
<input value="{{c.Cost}}" class="form-control" type="number" ng-model="c.Cost" ng-cloak/>
</td>
<td>
<input value="{{g.WeeklyValues|sumCalculation:'Cost'}}" class="form-control" string-to-number ng-model="Cost" type="number" ng-cloak/>
</td>
</tr>
</table>
Service:
app.factory("CalculationService", ['$http', function ($http) {
var CalculationService= {};
CalculationService.GetGroups = function () {
return $http.get("/Angular/GetGroups");
}
return CalculationService;}]);
What I want to achieve:
Calculate the sum of each value per group
Calculate the sum of all groups per week
Calculate the total sum of all groups
If i change the value of sum it should change the each weekly value accordingly
If i change the value of total (i.e. 1265 in example) it should recalculate the sum (value) of each group and each weekly value should be re-calculated:
The example of view is like:
GroupName------Week1Value------Week2Value------Week3Value------Sum
------------------ 312 ------------- 328 --------- 625----------- 1265
G1-------------- 112 ------------- 113 --------- 300 ---------- 525
G2-------------- 200 ------------- 215 --------- 325 ---------- 740

How to create sum of numbers from ng-repeat directive

I'm trying to sum a column of numbers but when I use code below, the "total" field is empty. Any thoughts on what I might be doing wrong?
HTML:
<tr ng-repeat="project in projectList.projects>
<td>{{project.description}}</td>
<td>{{project.type.cost | currency}}</td>
</tr>
<h2>Total: {{ total() | currency }}</h2>
Javascript:
myApp.controller('ProjectListCtrl', function ProjectListCtrl(Projects) {
var projectList = this;
projectList.total = function(){
var total = 0;
angular.forEach(projectList.projects, function(project) {
total += project.type.cost;
});
return total;
};
})
It appears that you're using the controllerAs syntax, such that ng-controller="ProjectListCtrl as projectList"
With that, you need to call projectList.total() from the view instead of just total()

Custom order using orderBy in ng-repeat

I have objects like this:
students = {name: 'Aa_Student', class: 'A_Class'},
{name: 'Ab_Student', class: 'A_Class'},
{name: 'Ac_Student', class: 'B_Class'},
{name: 'Ba_Student', class: 'B_Class'},
{name: 'Bb_Student', class: 'C_Class'},
{name: 'Bc_Student', class: 'C_Class'}
Let's say the students object is shuffled. I use ng-repeat to show the data. I want to sort the objects in the custom order.
For example, I want to show the data like this:
Name Class
-----------------------------
Ac_Student B_Class
Ba_Student B_Class
Aa_Student A_Class
Ab_Student A_Class
Bb_Student C_Class
Bc_Student C_Class
So basically, I want to order by student's class, but it B_Class comes first, then A_Class, then C_Class. Also, I want to order by students name in alphabetic order. How can I do this?
HTML:
<table>
<tr ng-repeat="student in students | orderBy:customOrder">
...
</tr>
</table>
Controller:
$scope.customOrder = function(student) {
$scope.students = $filter('orderBy')(student, function() {
});
};
Hi you can create custom sort filter please see here http://jsbin.com/lizesuli/1/edit
html:
<p ng-repeat="s in students |customSorter:'class'">{{s.name}} - {{s.class}} </p>
</div>
angularjs filter:
app.filter('customSorter', function() {
function CustomOrder(item) {
switch(item) {
case 'A_Class':
return 2;
case 'B_Class':
return 1;
case 'C_Class':
return 3;
}
}
return function(items, field) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (CustomOrder(a.class) > CustomOrder(b.class) ? 1 : -1);
});
return filtered;
};
});
Know this is old but may come in handy for others...
You could also create a simple custom sort function. "Not quite a filter":
$scope.customOrder = function (item) {
switch (item) {
case 'A_Class':
return 2;
case 'B_Class':
return 1;
case 'C_Class':
return 3;
}
};
And then use like you wanted to:
<table>
<tr ng-repeat="student in students | orderBy:customOrder">
...
</tr>
to set the orderBy as a property of the objects just quote that property name within the markup:
ng-repeat="student in students |orderBy:'name' | orderBy:'class'"
DEMO

AngularJs ng-repeat change loop increment

I'm newbie to AngularJs. I want to use ng-repeat as for(i=0; i < ele.length; i+=2)
I have a table with 4 columns, where I'm going to use ng-repeat
<table>
<tr ng-repeat="i in elements">
<th>{{i.T}}</th>
<td>{{i.V}}</td>
<th>{{elements[($index+1)].T}}</th> <!-- This should be next element of the elements array -->
<td>{{elements[($index+1)].V}}</td> <!-- This should be next element of the elements array -->
</tr>
</table>
I need to access 2 elements in a single iteration and iteration increment should be 2
I hope this make sense. Please help me.
Please check this html view Plunker
You can create a filter that creates an even copy of the array:
.filter("myFilter", function(){
return function(input, test){
var newArray = [];
for(var x = 0; x < input.length; x+=2){
newArray.push(input[x]);
}
return newArray;
}
});
JsFiddle: http://jsfiddle.net/gwfPh/15/
So if I understand correctly you want to walk your list and alternate th and td's while iterating.
If so you could use a ng-switch:
<table>
<tr ng-repeat="i in elements" ng-switch on="$index % 2">
<th ng-switch-when="0">{{i.T}}</th>
<td ng-switch-when="1">{{i.V}}</td>
</tr>
</table>
See Plunker here
One solution I can think of involves a change in the data model
Template
<table ng-app="test-app" ng-controller="TestCtrl">
<tr ng-repeat="i in items">
<th>{{i.T1}}</th>
<td>{{i.V1}}</td>
<th>{{i.T2}}</th>
<td>{{i.V2}}</td>
</tr>
</table>
Controller
testApp.controller('TestCtrl', ['$scope', function($scope) {
var elements=[]; //This is the dynamic values loaded from server
for (var i = 0; i < 5; i++) {
elements.push({
T : i,
V : 'v' + i
});
}
//A converter which convert the data elements to a format we can use
var items = [];
var x = Math.ceil(elements.length / 2);
for (var i = 0; i < x; i++) {
var index = i * 2;
var obj = {
T1 : elements[index].T,
V1 : elements[index].V
}
if (elements[index + 1]) {
obj.T2 = elements[index + 1].T;
obj.V2 = elements[index + 1].V
}
items.push(obj)
}
$scope.items = items;
}]);
Demo: Fiddle
Another slightly different approach can be found here.

Resources