How to use jQuery to find balance from Debit and Credit in Django row table

  django, jquery, python

Please help, i have my Django app where i have row table where fetch numbers from database and it work correctly, i have one problem i want to calculate balance by taking balance plus debit side and minus credit side by using jquery but refer what i get in a link plus what i expect.
Note: I failed to use django itself because balance doesn`t saved to database

What i get: [https://paste.pics/DW7TA]

Expected: [https://paste.pics/DW7WP]

javascript

< script >
  $(document).ready(function() {
    $('tr').each(function() {
      var balance = [];
      $(this).find('#col1').each(function() {
        var bal = $(this).text();
        if (bal.length !== 0) {
          var bala = $("#balance").text();
          balance = bal + bala
        }
      });
      $(this).find('#col2').each(function() {
        var bal = $(this).text();
        if (bal.length !== 0) {
          var bala = $("#balance").text();
          balance = bala - bal
        }
      });
      $(this).find("#balance").html(balance);
    });
  }); <
/script>

html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th>DR</th>
    <th>CR</th>
    <th>BALANCE</th>
  </tr>
  {% for statement in customer_stat %}
  <tr style="margin-top: 20px; font-size: 25px; margin-left: 90px;">
    <td class="col" id="col1">{{statement.total_amount}}</td>
    <td class="col" id="col2">{{statement.to_amount}}</td>
    <td id="balance"></td>
  </tr>
  {% endfor %}

</table>

Source: Python Questions

LEAVE A COMMENT