Add category of column

Asked

Viewed 502 times

1

Hello I am with this code and I would like to make the sum only of the category "CREDIT" someone gives a force for friend here.

$(document).ready(function () {
    var $entrada = 0,
        $saida = 0,
        $total = 0;
    $.each($("td[name='entrada']"), function() {
        $entrada += parseFloat($(this).text().replace(",", "."));
    });
    $.each($("td[name='saida']"), function() {
        $saida += parseFloat($(this).text().replace(",", "."));
    });
    $total = $entrada - $saida;
    $("#totalEntrada").append("R$ " + $entrada);
    $("#totalSaida").append("R$ " + $saida)
    $("#totalGeral").append("R$ " + $total);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
    <h2>ENTRADA</h2>
    <table width="198" border="1" id="table">
        <tr>
            <td width="39%">PRODUTO</td>
            <td width="12%">VALOR</td>
            <td width="12%">CATEGORIA</td>
        </tr>
        <tr>
            <td>1</td>
            <td name="entrada">100,00</td>
            <td name="CRED">PROVEDOR</td>
        </tr>
        <tr>
            <td>2</td>
            <td name="entrada">10,00</td>
            <td name="CREDITO">CREDITO</td>
        </tr>
    </table>
<h2>SAIDA</h2>
    <table width="196" border="1">
        <tr>
            <td width="39%">DESCRICAO</td>
            <td width="12%">VALOR</td>
        </tr>
        <tr>
            <td>SAIDA</td>
            <td name="saida">50,00</td>
        </tr>
    </table>
    <h2>TOTAL</h2>
<table border="1">
    <tr>
        <td>TOTAL ENTRADA</td>
        <td id="totalEntrada"></td>
    </tr>
    <tr>
        <td>TOTAL SAIDA</td>
        <td id="totalSaida"></td>
    </tr>
    <tr>
        <td>TOTAL GERAL</td>
        <td id="totalGeral"></td>
    </tr>
    <tr>
      <td>CREDITO</td>
      <td id="CREDITO"></td>
    </tr>
</table>
</body>

http://jsfiddle.net/hemerosn321/1qufobt3/3/

  • 1

    I didn’t get it right, what do you want to add with "CREDIT" (and that appears in the field "CREDIT" that is blank)? It is the credit plus the overall total (10+60 in the case)?

  • The totals are perfect I just want to separate the sum of the credits @gustavox

1 answer

1

Since the credit column has no number, the number/value itself is in the previous column so you have to use the .prev():

$.each($("td[name='CREDITO']"), function () {
    $credito += parseFloat($(this).prev().text().replace(",", "."), 0);
});

and then

$("#CREDITO").append("R$ " + $credito);

Stay like this:

$(document).ready(function () {
    var $entrada = 0,
        $saida = 0,
        $total = 0,
        $credito = 0;
    $.each($("td[name='entrada']"), function () {
        $entrada += parseFloat($(this).text().replace(",", "."));
    });
    $.each($("td[name='saida']"), function () {
        $saida += parseFloat($(this).text().replace(",", "."));
    });
    $.each($("td[name='CREDITO']"), function () {
        $credito += parseFloat($(this).prev().text().replace(",", "."), 0);
    });
    $total = $entrada - $saida;
    $("#totalEntrada").append("R$ " + $entrada);
    $("#totalSaida").append("R$ " + $saida)
    $("#totalGeral").append("R$ " + $total);
    $("#CREDITO").append("R$ " + $credito);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
     <h2>ENTRADA</h2>

    <table width="198" border="1" id="table">
        <tr>
            <td width="39%">PRODUTO</td>
            <td width="12%">VALOR</td>
            <td width="12%">CATEGORIA</td>
        </tr>
        <tr>
            <td>1</td>
            <td name="entrada">100,00</td>
            <td name="CRED">PROVEDOR</td>
        </tr>
        <tr>
            <td>2</td>
            <td name="entrada">10,00</td>
            <td name="CREDITO">CREDITO</td>
        </tr>
    </table>
    
<h2>SAIDA</h2>

    <table width="196" border="1">
        <tr>
            <td width="39%">DESCRICAO</td>
            <td width="12%">VALOR</td>
        </tr>
        <tr>
            <td>SAIDA</td>
            <td name="saida">50,00</td>
        </tr>
    </table>
     <h2>TOTAL</h2>

    <table border="1">
        <tr>
            <td>TOTAL ENTRADA</td>
            <td id="totalEntrada"></td>
        </tr>
        <tr>
            <td>TOTAL SAIDA</td>
            <td id="totalSaida"></td>
        </tr>
        <tr>
            <td>TOTAL GERAL</td>
            <td id="totalGeral"></td>
        </tr>
        <tr>
            <td>CREDITO</td>
            <td id="CREDITO"></td>
        </tr>
    </table>
</body>

jsFiddle: http://jsfiddle.net/c7pzjzfg/

Browser other questions tagged

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