Sum values of a column td

Asked

Viewed 397 times

0

I would like to know how to calculate the values of a column and play in a field. They are automatically loaded

This is the table html. I want to add the column values valuables and play on the field qtdtotal just below.

<table class="table table-md-striped table-condensed" id="tblImpostos" tablename="tblImpostos" noaddbutton="true" nodeletebutton="true">
    <thead>
        <tr>
            <th>Ano Competência</th>
            <th>Mês Competência</th>
            <th>Desconto</th>
            <th>Valor</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input readonly type="text" class="form-control input-sm" name="anocompetenciaimpostos" id="anocompetenciaimpostos">
            </td>
            <td>
                <input readonly type="text" class="form-control input-sm" name="mescompetenciaimpostos" id="mescompetenciaimpostos">
            </td>

            <td>
                <input readonly type="text" class="form-control input-sm" name="descontoimpostos" id="descontoimpostos">
            </td>
            <td>
                <input readonly type="text" class="form-control input-sm valor-calculado" name="valorimpostos" id="valorimpostos">
            </td>

        </tr>
    </tbody>
</table>

<input readonly type="text" class="form-control input-sm" name="qtdtotal" id="qtdtotal">

This is the javascript that loads the data in the table

function getImpostos(){

    if ($("#nmQtdListaImpostos").val() == "0"){
        var chapafuncionario = $('#chapafuncionario').val();
        var anoreferencia = $('#anoreferencia').val();
        var mesreferencia = $('#mesreferencia').val();
        var c1 = DatasetFactory.createConstraint("chapa", chapafuncionario, chapafuncionario, ConstraintType.MUST);
        var c2 = DatasetFactory.createConstraint("anocomp", anoreferencia, anoreferencia, ConstraintType.MUST);
        var c3 = DatasetFactory.createConstraint("mescomp", mesreferencia, mesreferencia, ConstraintType.MUST);
        var constraints   = new Array(c1, c2, c3);
        var dataset = DatasetFactory.getDataset("dsImpostos", null, constraints, null);

        for(var i = 0; i < dataset.values.length; i++) {
            row = dataset.values[i];
            linha = wdkAddChild('tblImpostos');

            $("#nmQtdListaImpostos").val(linha);

            $("#anocompetenciaimpostos___" + linha).val(row["ANOCOMP"]);
            $("#mescompetenciaimpostos___" + linha).val(row["MESCOMP"]);
            $("#descontoimpostos___" + linha).val(row["DESCRICAO"]);
            $("#valorimpostos___" + linha).val(row["VALOR"]);
        }   
    }
}

2 answers

1

You can add these values while filling the table and right after displaying this result

var total = 0;
for(var i = 0; i < dataset.values.length; i++) {
    row = dataset.values[i];
    linha = wdkAddChild('tblImpostos');
    $("#nmQtdListaImpostos").val(linha);

    $("#anocompetenciaimpostos___"+linha).val(row["ANOCOMP"]);
    $("#mescompetenciaimpostos___"+linha).val(row["MESCOMP"]);
    $("#descontoimpostos___"+linha).val(row["DESCRICAO"]);
    $("#valorimpostos___"+linha).val(row["VALOR"]);

    total += row["VALOR"];
}   
$("#qtdtotal").val(total);

0

Hello! follow the function below, just put it on your page and call when you want, it should work!

function calculaFinal(){
    var valorFinal = 0;
    //pega todos os campos de imposto dentro da table
    $("#tblImpostos input[name*='valorimpostos']").toArray().forEach(function(element,index){
        valorFinal += parseInt(element.value);
    })
    //aplica no valor final
    $('#qtdtotal').val(valorFinal)
}

If you solve your problem, don’t forget to mark it as the correct answer, as this way you help other people with the same problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.