Reputation: 21
Is it possible to calculate the average grade.. of the tables? I only found answers with input fields. I want to show the average at the footer in "totalAvg". I don't know if it's possible with a jQuery-Plugin?
<table class="kader">
<tbody>
<tr class="head">
<th colspan="1">Nr</th>
<th colspan="1">Gegner</th>
<th colspan="1">Tore</th>
<th colspan="1"><span class="positionc pos_11">11</span></th>
<th colspan="1">Asissts</th>
<th colspan="1"><span class="positionc pos_yc"></span></th>
<th colspan="1"><span class="positionc pos_yc"></span> <span class="positionc pos_rc"></span></th>
<th colspan="1"><span class="positionc pos_rc"></span></th>
<th colspan="1">Grade 1</th>
<th colspan="1">Grade 2</th>
<th colspan="1">Grade 3</th>
</tr>
</tbody>
<tbody>
<tr>
<td>1.</td>
<td>FC Augsburg</a></td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note"><span class="position pos_y">3</span></td>
<td class="mini_note"><span class="position pos_y">3,5</span></td>
<td class="mini_note"><span class="position pos_y">3</span></td>
</tr>
<tr>
<td>2.</td>
<td>1899 Hoffenheim</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note">-</td>
<td class="mini_note"><span class="position pos_y">4</span></td>
<td class="mini_note"><span class="position pos_r">4,5</span></td>
<td class="mini_note"><span class="position pos_y">4</span></td>
</tr>
</tbody>
<tbody>
<tr class="footer">
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1"></th>
<th colspan="1" class="totalAvg"></th>
<th colspan="1" class="totalAvg"></th>
<th colspan="1" class="totalAvg"></th>
</tr>
</tbody>
</table>
Upvotes: 0
Views: 829
Reputation: 27833
Here is some piece of code you need to change to fit your exact needs.
var sums = [];
var counts = [];
$('tr:not(.head)').each(function(){
$(this).children('td').each(function(index){
var value = +$(this).text().replace(',', '.');
sums[index] = (sums[index] || 0) + (isNaN(value) ? 0 : value);
counts[index] = (counts[index] || 0) + (isNaN(value) ? 0 : 1);
});
});
$('table tbody').last().append('<tr id="averages">');
sums.forEach(function(item, index) {
$('#averages').append($('<td>').html(sums[index]/counts[index]));
});
Demo: http://jsbin.com/oMOWOmo/2/edit
PS: I know it computes the average for the Number column. I thought it was better this way.
PSS: If your table was structured better, the solution would be much simpler and straightforward.
Upvotes: 1