It maybe sounds weird but based in the following table I need to sum all rows by using jQuery. I'm using angularjs to list all users but I need to get each row with jQuery and then show the sum of all them.
The only problem is I don't know how to get the value of each row in order to make the operation.
HTML
<div data-ng-app="app" data-ng-controller="controllerApp">
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="user in users">
<td>{{user.name}}</td>
<td class="sum">{{user.age}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td><strong>Sum</strong></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
Javascript
$(function(){
function tally (selector) {
$(selector).each(function () {
var total = 0,
column = $(this).siblings(selector).andSelf().index(this);
$(this).parents().prevUntil(':has(' + selector + ')').each(function () {
total += parseFloat($('td.sum:eq(' + column + ')', this).html()) || 0;
})
$(this).html(total);
});
}
tally('td.total');
});
Angularjs
var app = angular.module("app", []);
app.controller("controllerApp", function($scope){
$scope.users = [
{name: "Marie", age: 24},
{name: "Andy", age: 26},
{name: "Peter", age: 21},
{name: "Elizabeth", age: 23},
{name: "Frank", age: 27},
{name: "Claire", age: 25}
]
});