2013-11-15 4 views
0

테이블의 평균 등급을 계산할 수 있습니까? 입력란에 대해서만 답변을 찾았습니다. "totalAvg"의 바닥 글에 평균을 표시하고 싶습니다. 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> 

답변

1

정확한 요구 사항에 맞게 변경해야하는 코드가 있습니다.

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

데모 : http://jsbin.com/oMOWOmo/2/edit

PS : 나는 숫자 컬럼의 평균을 계산 알고있다. 나는 그것이 그것이 더 좋은 방법이라고 생각했다.

PSS : 테이블을 더 잘 구성하면 솔루션이 훨씬 간단하고 직관적입니다.