Add column category

1

Hello, I have this code and I would like to add only the "CREDIT" category to someone from a force to my 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><tablewidth="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>

link

    
asked by anonymous 06.05.2015 / 05:40

1 answer

1

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

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

and then

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

It looks 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><tablewidth="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: link

    
06.05.2015 / 07:20