简体   繁体   English

统计列数,表Jquery中的行

[英]Count cols, rows in table Jquery

I have a table like this: 我有一张这样的桌子: 在此处输入图片说明

How I can calculate lines (level1 + level2 ... level5) and rows (level1 + next line level1...) for total row in end of the table? 如何计算表末尾总行的行(level1 + level2 ... level5)和行(level1 +下一行level1 ...)?

For total rows I use this: 对于总行,我使用以下代码:

function totalRow()
{
    var total = 0;
    $('#test').each(function(){
        $('.input-md').each(function(){
            if ($(this).val() != '') {
                total += parseInt($(this).val());
            }
        });
    });
    $('#totalall').val(total);
}

html id s must be unique in the document. html id在文档中必须是唯一的。 Classes may be duplicated. 类可以重复。

$('#test').each(function(){ doesn't make sense, because there can only be one id #test in the document. It is not looping across your rows. $('#test').each(function(){没有意义,因为在文档中只能有一个id #test,它不会遍历您的行。

I think the point is that you forgot to select #primary . 我认为关键是您忘记选择#primary You may write instead : 您可以改写:

$('#test,#primary').each(function(){
    $('.input-md').each(function(){
        if ($(this).val() != '') {
            total += parseInt($(this).val());
        }
    });
});

But if you use the class input-md only in this table, then you can simplify it by writing : 但是,如果仅在此表中使用类input-md ,则可以通过编写以下代码来简化它:

$('.input-md').each(function(){
    if ($(this).val() != '') {
        total += parseInt($(this).val());
    }
});

I solve my problem. 我解决了我的问题。 For count rows : 对于计数行:

function totalRow()
{
    var total = 0;
    $('table .count-row td .cols, .total-row').each(function() {
        if ($(this).prop('disabled')) {
            $(this).val(total);
            total = 0;
        } else {
            if ($(this).val() != '') {
                total += parseInt($(this).val());
            }
        }
    });
}

For count columns: 对于计数列:

function totalColumns()
{
    var id = ["one", "two", "three", "four", "five"];
    var totalCol = 0;
    for (var i=0; i<=5; i++) {
        $('.column-' + id[i]).each(function(){
            if ($(this).val() != '') {
                totalCol += parseInt($(this).val());
            }
            $('#count-' + id[i]).text(totalCol);
        });
        totalCol = 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM