I have a table that is automatically generated by PHP and manipulated by jquery, but I need to calculate the percentage of the total of each of the number lines, what is the best way to do this?
<div id="tableResults" class="container">
<table id="table_resultados" class="table table-bordered table-hover">
<thead class="table-head">
<tr>
<th>SEX</th>
<th class="names">FATAL</th>
<th class="names">SERIOUS</th>
<th class="names">OTHERS</th>
<th class="names">TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>WOMAN</td>
<td class="VitimaFATAL">4</td>
<td class="SERIOUS">6</td>
<td class="OTHERS">1</td>
<td class="Total">11</td>
</tr>
<tr>
<td>MEN</td>
<td class="FATAL">18</td>
<td class="SERIOUS">28</td>
<td class="OTHERS">23</td>
<td class="Total">69</td>
</tr>
<tr>
<td>UNKNOWN</td>
<td class="FATAL">3</td>
<td class="SERIOUS">1679</td>
<td class="OTHERS">3129</td>
<td class="Total">4811</td>
</tr>
<tr id="Totais">
<td>TOTAL</td>
<td id="FATAL">25</td>
<td id="SERIOUS">1713</td>
<td id="OTHERS">3153</td>
<td id="Total">4891</td>
</tr>
</tbody>
</table>
</div>
This table comes from a dynamic search in PHP and MYSQL that generates the values, I already did a function in jQuery to calculate the total
function colSum() {
var ids = new Array();
$('#table_resultados .names').each(function (){
ids.push($(this).html().replace(/\s/g, ''));
$('#Totais').append('<td id="' + $(this).html().replace(/\s/g, '') + '"></td>')
});
$.each(ids, function (index, value) {
var sum = 0;
$('.' + value).each(function () {
sum += parseInt($(this).html());
});
$('#' + value).html(sum);
});
I need to calculate the percentage of the total of all the columns. In this example I need to insert a column after the FATAL, SERIOUS, OUTHERS call % of the total and with values, eg:
- the% column of FATAL total would be 36.36 - 26.08 - 0.06 - 0.51
- the% SERIOUS total column would be 54.54 - 40.57 - 34.89 - 35.02
The account to be made is the value of the column multiplied by 100 divided by the value of the total of the same line as the first line 4 * 100/11
I created HTML to stay pretty much as I need it, I do not know how to dynamically generate it for all lines
<div id="tableResults" class="container">
<table id="table_resultados" class="table table-bordered table-hover">
<thead class="table-head">
<tr>
<th>SEX</th>
<th class="names">FATAL</th>
<th>% do TOTAL</th>
<th class="names">SERIOUS</th>
<th class="names">OTHERS</th>
<th class="names">TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>WOMAN</td>
<td class="VitimaFATAL">4</td>
<th>36,36</th>
<td class="SERIOUS">6</td>
<td class="OTHERS">1</td>
<td class="Total">11</td>
</tr>
<tr>
<td>MEN</td>
<td class="FATAL">18</td>
<th>26,08</th>
<td class="SERIOUS">28</td>
<td class="OTHERS">23</td>
<td class="Total">69</td>
</tr>
<tr>
<td>UNKNOWN</td>
<td class="FATAL">3</td>
<th>0,06</th>
<td class="SERIOUS">1679</td>
<td class="OTHERS">3129</td>
<td class="Total">4811</td>
</tr>
<tr id="Totais">
<td>TOTAL</td>
<td id="FATAL">25</td>
<th>0,51</th>
<td id="SERIOUS">1713</td>
<td id="OTHERS">3153</td>
<td id="Total">4891</td>
</tr>
</tbody>
</table>
</div>