总和为列 [英] SUM Total for Column

查看:109
本文介绍了总和为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考previous的问题在这里:总和总数列在jQuery的

我用Aymen的解决方案,但我编辑它适合我的需要。它停止了工作,我的$ C $截至的jsfiddle看到如下C: http://jsfiddle.net/unKDk/ 15 /

 <表ID =sum_tableWIDTH =300的边界=1>
    &所述; TR类=titlerow>
        < TD>苹果< / TD>
        < TD>橙色与LT; / TD>
        < TD>西瓜< / TD>
        < TD>草莓< / TD>
        < TD>合计按行< / TD>
    < / TR>
    &其中; TR>
        &所述; TD类=rowAA→1&其中; / TD>
        &所述; TD类=rowAA→2&其中; / TD>
        &所述; TD类=rowBB→3&所述; / TD>
        &所述; TD类=rowBB→4&所述; / TD>
        < TD类=totalRow>< / TD>
    < / TR>
    &其中; TR>
        &所述; TD类=rowAA→1&其中; / TD>
        &所述; TD类=rowAA→2&其中; / TD>
        &所述; TD类=rowBB→3&所述; / TD>
        &所述; TD类=rowBB→4&所述; / TD>
        < TD类=totalRow>< / TD>
    < / TR>
    &其中; TR>
        &所述; TD类=rowAA→1&其中; / TD>
        &所述; TD类=rowAA→5&所述; / TD>
        &所述; TD类=rowBB→3&所述; / TD>
        &所述; TD类=rowBB→4&所述; / TD>
        < TD类=totalRow>< / TD>
    < / TR>
    &所述; TR类=totalColumn>
        < TD类=totalCol个总:LT; / TD>
        < TD类=totalCol个总:LT; / TD>
        < TD类=totalCol个总:LT; / TD>
        < TD类=totalCol个总:LT; / TD>
        < TD类=totalCol个总:LT; / TD>
    < / TR>
< /表>
 

jQuery的部分是

  VAR合计= [0,0,0,0,0];
$(文件)。就绪(函数(){

    变量$数据行= $(#sum_table TR:不是('totalColumn,.titlerow'));

    $ dataRows.each(函数(){
        $(本).find('。rowAA')。每个(函数(一){
            总计[I] + = parseInt函数($(本)。html的());
        });

        $(本).find('。rowBB')。每个(函数(一){
            总计[I] + = parseInt函数($(本)。html的());
        });
    });
    $(#sum_table td.totalCol)。每个(函数(一){
        $(本)。html的(总:+总计[I]);
    });

});
 

  1. 如何解决导致jQuery的计算错误的问题。
  2. 如何按行计算总
  3. 我需要的类名完全相同。
解决方案

我不太清楚你想要什么,但如果你只是想总和列的所有行则见下文。

  VAR totalsByRow = [0,0,0,0,0];
变种totalsByCol = [0,0,0,0,0];
$(文件)。就绪(函数(){

    变量$数据行= $(#sum_table TR:不是('totalColumn,.titlerow'));

    $ dataRows.each(功能(我){
        $(本).find('TD:没有(.totalRow)')。每个(函数(J){
            totalsByCol [J] + = parseInt函数($(本)。html的());
            totalsByRow [I] + = parseInt函数($(本)的.html());
        });
    });

    对于(VAR I = 0; I< totalsByCol.length  -  1;我++){
        totalsByCol [totalsByCol.length  -  1] + = totalsByCol [I]
    }

    $(#sum_table td.totalCol)。每个(函数(一){
        $(本)。html的(总:+ totalsByCol [I]);
    });

    $(#sum_table td.totalRow)。每个(函数(一){
        $(本)。html的(总:+ totalsByRow [I]);
    });
});
 

DEMO

please refer to previous question here: Sum total for column in jQuery

i used Aymen's solution, but i edited it to suite my need. It stopped working, my code as below as seen at jsfiddle: http://jsfiddle.net/unKDk/15/

<table id="sum_table" width="300" border="1">
    <tr class="titlerow">
        <td>Apple</td>
        <td>Orange</td>
        <td>Watermelon</td>
        <td>Strawberry</td>
        <td>Total By Row</td>
    </tr>
    <tr>
        <td class="rowAA">1</td>
        <td class="rowAA">2</td>
        <td class="rowBB">3</td>
        <td class="rowBB">4</td>
        <td class="totalRow"></td>
    </tr>
    <tr>
        <td class="rowAA">1</td>
        <td class="rowAA">2</td>
        <td class="rowBB">3</td>
        <td class="rowBB">4</td>
        <td class="totalRow"></td>
    </tr>
    <tr>
        <td class="rowAA">1</td>
        <td class="rowAA">5</td>
        <td class="rowBB">3</td>
        <td class="rowBB">4</td>
        <td class="totalRow"></td>
    </tr>
    <tr class="totalColumn">
        <td class="totalCol">Total:</td>
        <td class="totalCol">Total:</td>
        <td class="totalCol">Total:</td>
        <td class="totalCol">Total:</td>
        <td class="totalCol">Total:</td>
    </tr>
</table>

Jquery part is

var totals=[0,0,0,0,0];
$(document).ready(function(){

    var $dataRows=$("#sum_table tr:not('.totalColumn, .titlerow')");

    $dataRows.each(function() {
        $(this).find('.rowAA').each(function(i){        
            totals[i]+=parseInt( $(this).html());
        });

        $(this).find('.rowBB').each(function(i){        
            totals[i]+=parseInt( $(this).html());
        });        
    });
    $("#sum_table td.totalCol").each(function(i){  
        $(this).html("total:"+totals[i]);
    });

});

  1. how to solve the problem that caused the jquery calculate wrongly.
  2. how to calculate total by row
  3. i need the class name exactly same.

解决方案

I am not quite sure what you want, but if you just want to sum all rows by column then see below..

var totalsByRow = [0, 0, 0, 0, 0];
var totalsByCol = [0, 0, 0, 0, 0];
$(document).ready(function() {

    var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");

    $dataRows.each(function(i) {
        $(this).find('td:not(.totalRow)').each(function(j) {
            totalsByCol[j] += parseInt($(this).html());
            totalsByRow[i] += parseInt($(this).html());
        });
    });

    for (var i = 0; i < totalsByCol.length - 1; i++) {
        totalsByCol[totalsByCol.length - 1] += totalsByCol[i];       
    }    

    $("#sum_table td.totalCol").each(function(i) {
        $(this).html("total:" + totalsByCol[i]);
    });

    $("#sum_table td.totalRow").each(function(i) {
        $(this).html("total:" + totalsByRow[i]);
    });
});

DEMO

这篇关于总和为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆