计算表中每行总数的百分比

时间:2017-04-25 14:00:56

标签: javascript jquery

我有一个由PHP自动生成并由jquery操作的表,所以我需要计算每个数字行总数的百分比,这是最好的方法吗?

<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>

我已经能够将所有值相加

        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);


        });

我需要计算此示例中所有列的总数百分比我需要在FATAL,SERIOUS,OUTHERS调用'%of total'之后插入一列,并且值为ex:%column of FATAL total 36.36 - 26.08 - 0.06 - 0.51总SERIOUS的%列为54.54 - 40.57 - 34.89 - 35.02

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
var total_per = 0;
$.each($('#table_resultados td.Total'), function() {  
  if (!isNaN($(this).text())) {
    total_per = total_per + parseInt($(this).text());
  }
});
console.log('Total: ' + total_per);

$.each($('#table_resultados tbody tr'), function () {
  var obj = $(this).find('.Total');
  if (!isNaN(obj.text())) {
     var v = parseInt(obj.text());

     $('<td></td>')
     .text( parseFloat((v / total_per) * 100).toFixed(2) + '%' )
     .appendTo(this);
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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 class="FATAL">25</td>
        <td class="SERIOUS">1713</td>
        <td class="OTHERS">3153</td>
        <td class="Total">4891</td>
      </tr>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;

你会在这里注意到:

 <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>

在所有这些表格单元格上id时,class为{{1}}。 当你需要一定比例的东西时,你取数字并除以总数,然后乘以100得到百分比。