【问题标题】:Calculate average in a table计算表格中的平均值
【发布时间】:2013-11-15 16:56:24
【问题描述】:

可以计算表格的平均成绩吗? 我只找到了输入字段的答案。 我想在“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>

【问题讨论】:

    标签: javascript html-table average


    【解决方案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:我知道它会计算 Number 列的平均值。我认为这样更好。

    PSS:如果您的表格结构更好,解决方案会更加简单明了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2013-01-14
      相关资源
      最近更新 更多