JQuery Loop через ячейки таблицы, затем строки - каждый()

Я немного застреваю, пытаясь пропустить строку таблицы, вычислить значения (которые затем выводятся), а затем перейти к следующей строке. Кажется, отлично работает для первой строки?

Здесь фрагмент кода и ссылка jsfiddle:

var rows = $("#invoice_table tr:gt(0)"); // skip the header row

        rows.each(function(index) {
            rows.children("td").each(function() { // calculate amount, vat, subtotal for row
                qty = $("td:nth-child(3) input").val();
                rate = $("td:nth-child(4) input").val();
                amount = qty * rate;
                vat = amount / 100 * 20;
                subtotal = amount;
                subtotal += vat;

                vat = vat.toFixed(2); // limit to two decimal places
                amount = amount.toFixed(2);
                subtotal = subtotal.toFixed(2);

                $("td:nth-child(5) input").val(amount); // output the values
                $("td:nth-child(6) input").val(vat);
                $("td:nth-child(7) input").val(subtotal);
            });
        });

http://jsfiddle.net/8QK2E/48/

Спасибо!

ИЗМЕНИТЬ

Хорошо, поэтому, будучи упрямым человеком, я был уверен в своих собственных идеях. Это код, в котором я оказался. Вычисляет строки, затем добавляет столбцы.

$("#invoice_table").live({ // invoice calculations
    blur: function() {
    var qty = '0.0';
    var rate = '0.0';
    var amount = '0.0';
    var amttotal = '0.0';
    var vat = '0.0';
    var vattotal = '0.0';
    var subtotal = '0.0';
    var total = '0.0';
    var num_tr = $('#invoice_table tr').length;
    num_tr = num_tr-2; // skip totals row

    amount = amount * 1; // force js to handle the var as a number
    qty = qty * 1;
    rate = rate * 1;
    vat = vat * 1;
    amttotal = amttotal * 1;            
    vattotal = vattotal * 1;   
    total = total * 1;

    for(var i = 1;i < num_tr;i++) {
    var row = $('#invoice_table tr:nth-child('+i+')'); // skip the header row

    row.children("td").each(function() { // calculate amount, vat, subtotal for row
    qty = $('tr:nth-child('+i+') td:nth-child(3) input').val();
    rate = $('tr:nth-child('+i+') td:nth-child(4) input').val();
    amount = qty * rate;
    vat = amount / 100 * 20;
    subtotal = amount;
    subtotal += vat;

    vat = vat.toFixed(2); // limit to two decimal places
    amount = amount.toFixed(2);
    subtotal = subtotal.toFixed(2);

    $('tr:nth-child('+i+') td:nth-child(5) input').val(amount); // output the values
    $('tr:nth-child('+i+') td:nth-child(6) input').val(vat);
    $('tr:nth-child('+i+') td:nth-child(7) input').val(subtotal);
    });

    }

    for(var i = 2;i < num_tr;i++) {
    var rows = $('#invoice_table tr:nth-child('+i+')'); // skip the header row

    amttotal += parseFloat($('tr:nth-child('+i+') td:nth-child(5) input').val());
    vattotal += parseFloat($('tr:nth-child('+i+') td:nth-child(6) input').val());
    total    += parseFloat($('tr:nth-child('+i+') td:nth-child(7) input').val());
    }

    amttotal = amttotal.toFixed(2); // limit to two decimal places
        vattotal = vattotal.toFixed(2);
        total = total.toFixed(2);

        $("#total_amount input").val(amttotal);
        $("#total_vat input").val(vattotal);
        $("#grand_total input").val(total);
    }
});

Ответы

Ответ 1

Сначала я вижу проблему, в вашем первом rows.each вы затем переходите через all td, которые являются дочерними элементами rows. Вместо этого используйте this, чтобы ограничить его текущей текущей строкой. Кроме того, у вас нет возможностей для частей qty и rate, вы также смотрите все td.

Вам может потребоваться настроить селектор как материал для выходов, поскольку я действительно не знаю ваш макет или где вы пытаетесь отобразить выходы.

var rows = $("#invoice_table tr:gt(0)"); // skip the header row

    rows.each(function(index) {
        var qty_input = $("td:nth-child(3) input", this);
        var rate_input = $("td:nth-child(4) input", this);
        $(this).children("td").each(function() { // calculate amount, vat, subtotal for row
            var qty = qty_input .val();
            var rate = rate_input .val();
            var amount = qty * rate;
            var vat = amount / 100 * 20;
            var subtotal = amount;
            subtotal += vat;

            vat = vat.toFixed(2); // limit to two decimal places
            amount = amount.toFixed(2);
            subtotal = subtotal.toFixed(2);

            $('#total_amount').val(amount); // output the values
            $('#total_vat').val(vat);
            $('#grand_total').val(subtotal);
        });
    });

Потенциально, однако, вы можете привязываться к событиям изменений для каждого поля ввода и выполнять только вычисления. Если вы можете редактировать свой источник, чтобы добавить еще несколько классов, вы можете сделать что-то вроде:

var inputs = $('#invoice_table tr:gt(0) td.quantity input, #invoice_table tr:gt(0) td.rate input');
var amount_inputs = $('#invoice_table tr:gt(0) td.amount input');
var vat_inputs = $('#invoice_table tr:gt(0) td.vat input');
var subtotal_inputs = $('#invoice_table tr:gt(0) td.subtotal input');

inputs.change(function() {
    // Finding them this way purely so that we don't duplicate the code,
    // it would be faster to separate the inputs array into their type and bind the change appropriately.
    var qty = $(this).closest('tr').find('td.quantity input').val();
    var rate = $(this).closest('tr').find('td.rate input').val();
    var amount_input = $(this).closest('tr').find('td.amount input');
    var vat_input = $(this).closest('tr').find('td.vat input');
    var subtotal_input = $(this).closest('tr').find('td.subtotal input');

    var amount = qty * rate;
    var vat = amount / 100 * 20;
    var subtotal = amount + vat;

    amount_input.val(amount.toFixed(2));
    vat_input.val(vat.toFixed(2));
    subtotal_input.val(subtotal.toFixed(2));

    update_totals();
});

function update_totals() {
    var amttoal = 0;
    var vattotal = 0;
    var total = 0;

    amount_inputs.each(function(){
        amttoal += this.val();
    });
    vat_inputs.each(function(){
        vattotal += this.val();
    });
    subtotal_inputs.each(function(){
        vattotal += this.val();
    });

    $("#total_amount input").val(amttoal.toFixed(2));
    $("#total_vat input").val(vattotal).toFixed(2);
    $("#grand_total input").val(vattotal.toFixed(2));
}

Не уверен, что было бы быстрее или проще.