Jquery:动态循环表的列和总和

时间:2017-05-31 02:02:36

标签: javascript jquery

我有这些情景,我想总结一下

1)单位x 表A 单位x 表B

2)单位y 表A 单位y 表B

3)将总和按表A 上的行索引表C

以下是我的代码,工作正常:

表A:

<table class="tableA" border='1'>
    <tbody>
        <tr>
            <td></td>
            <td>JAN</td>
            <td>FEB</td>
            <td>MAR</td>
        </tr>
        <tr>
            <td>Unit X</td>
            <td><input type="text" value="1" class="jan"></td>
            <td><input type="text" value="2" class="feb"></td>
            <td><input type="text" value="3" class="mar"></td>
        </tr>
        <tr>
            <td>Unit Y</td>
            <td><input type="text" value="2" class="jan"></td>
            <td><input type="text" value="2" class="feb"></td>
            <td><input type="text" value="2" class="mar"></td>
        </tr>
    </tbody>
</table>

表B:

<table class="tableB" border='1'>
    <tbody>
        <tr>
            <td></td>
            <td>JAN</td>
            <td>FEB</td>
            <td>MAR</td>
        </tr>
        <tr>
            <td>Unit X</td>
            <td><input type="text" value="4" class="jan"></td>
            <td><input type="text" value="4" class="feb"></td>
            <td><input type="text" value="4" class="mar"></td>
        </tr>
        <tr>
            <td>Unit Y</td>
            <td><input type="text" value="5" class="jan"></td>
            <td><input type="text" value="5" class="feb"></td>
            <td><input type="text" value="5" class="mar"></td>
        </tr>
    </tbody>
</table>

表C:

<table class="tableC" border='1'>
    <tbody>
        <tr>
            <td></td>
            <td>JAN</td>
            <td>FEB</td>
            <td>MAR</td>
        </tr>
        <tr>
            <td>Unit X</td>
            <td><input type="text" value="" class="jan"></td>
            <td><input type="text" value="" class="feb"></td>
            <td><input type="text" value="" class="mar"></td>
        </tr>
        <tr>
            <td>Unit Y</td>
            <td><input type="text" value="" class="jan"></td>
            <td><input type="text" value="" class="feb"></td>
            <td><input type="text" value="" class="mar"></td>
        </tr>
    </tbody>
</table>

Jquery的:

$( document ).ready(function() {

    $(".tableA").find(".jan").each(function() { 
        var value_A = $(this).val();
        var row_index = $(this).closest("tr").index();
        var value_B = $('.tableB').find("tr:eq("+row_index+") .jan").val();
        var sum = parseInt(value_A) + parseInt(value_B);
        $('.tableC').find("tr:eq("+row_index+") .jan").val(sum);
    }); 
    $(".tableA").find(".feb").each(function() { 
        var value_A = $(this).val();
        var row_index = $(this).closest("tr").index();
        var value_B = $('.tableB').find("tr:eq("+row_index+") .feb").val();
        var sum = parseInt(value_A) + parseInt(value_B);
        $('.tableC').find("tr:eq("+row_index+") .feb").val(sum);
    });
    $(".tableA").find(".mar").each(function() { 
        var value_A = $(this).val();
        var row_index = $(this).closest("tr").index();
        var value_B = $('.tableB').find("tr:eq("+row_index+") .mar").val();
        var sum = parseInt(value_A) + parseInt(value_B);
        $('.tableC').find("tr:eq("+row_index+") .mar").val(sum);
    });    
});

JSFiddle

问题:当前的sum方法按月份的类名进行硬编码,例如:.jan .feb .mar

如何动态地通过其列进行循环而不对类名进行硬编码,因为代码将在12月之前变得更长。

感谢

2 个答案:

答案 0 :(得分:1)

每个函数的代码通常是相同的(差异实际上只是月份。我们可以将它作为通用函数。即

const processMonth = function($table, month) { 
    // Assume month = jan/feb/mar/...
    var value_A = $table.val();
    var row_index = $table.closest("tr").index();
    var value_B = $('.tableB').find("tr:eq("+row_index+") ." + month).val();
    var sum = parseInt(value_A) + parseInt(value_B);
    $('.tableC').find("tr:eq("+row_index+") ." + month).val(sum);
});

现在我们可以做类似

的事情了
// Some months for brevity
const months = ["jan", "feb", "mar", "apr", ... "nov", "dec"];

months.forEach(function(month) {
    processMonth($(".tableA"), month);
});

答案 1 :(得分:1)

假设每个TD只有输入[type =“text”],那么您可以执行以下操作。如果需要,可以使用类选择器。

$('.tableC input[type="text"]').each(function() {
    var indexTR = $(this).closest('tr').index();
    var indexTD = $(this).closest('td').index();

    var dataA = $('.tableA tr:eq(' + indexTR + ') td:eq(' + indexTD + ') input[type="text"]').val();
    var dataB = $('.tableB tr:eq(' + indexTR + ') td:eq(' + indexTD + ') input[type="text"]').val();

    var dataC = parseInt(dataA) + parseInt(dataB);
    $(this).val(dataC);
});