Search code examples
javascriptjqueryhtml

How to sum dynamically added column values?


I have dynamically added rows that I need to sum. Added columns and total will be displayed in separate fields.

I want to sum user enter data one particular column that is "DEBIT*" columns. For example user enter 1st row on debit column 100rs and user click add new row and enter 100rs that amount will be calculate and show the total field.

Here is my code:

Failed Fiddle here

var ctr = 1;
var FieldCount = 1;
$('#fst_row').on('click', '.button-add', function() {
  ctr++;
  var cashacc_code = 'cashacc_code' + ctr;
  var cashacc = 'cashacc' + ctr;
  var cash_narrat = 'cash_narrat' + ctr;
  var cashdeb = 'cashdeb' + ctr;
  var cashcredit = 'cashcredit' + ctr;
  var newTr = '<tr class="jsrow"><td><input type="number" class=' + "joe" + ' id=' + cashacc_code + ' placeholder="NNNN" /></td><td><select class="form-control" id="cashacc" ><option value="">TDS A/C Name1</option><option value="1">Joe</option><option value="2">Joe</option><option value="3">Joe</option></select></td><td><input type="text" class=' + "joe" + ' id=' + cash_narrat + ' placeholder="Enter Here" /></td><td><input type="number" class=' + "joe" + ' id=' + cashdeb + ' ' + FieldCount + ' placeholder="NNNN" /></td><td><input type="number" class=' + "joe" + ' id=' + cashcredit + ' /></td><td style="width: 4%"><img src="./img/plus.svg" class="insrt-icon button-add"><img src="./img/delete.svg" class="dlt-icon"></td></tr>';
  $('#cashTable').append(newTr);

  $(document).on('click', '.dlt-icon', function() {
    $(this).parents('tr.jsrow').first().remove();
  });
});


/* second row  */

var ctr = 1;
var FieldCount = 1;
$('#sndRow').on('click', '.button-add', function() {
  ctr++;
  var rowNum = 'rowNum' + ctr;
  var cashacc_nme = 'cashacc_nme' + ctr;
  var acc_narrat = 'acc_narrat' + ctr;
  var accdeb = 'accdeb' + ctr;
  var accCredit = 'accCredit' + ctr;
  var newTr = '<tr class="jsrow"><td><input type="number" class=' + "joe" + ' id=' + rowNum + ' placeholder="NNNN" /></td><td><select class="form-control" id="cashacc_nme" ><option value="">Account Name 1</option><option value="1">Plumz</option><option value="2">Plumz</option><option value="3">Plumz</option></select></td><td><input type="text" class=' + "joe" + ' id=' + acc_narrat + ' placeholder="Enter Here" /></td><td><input type="number" class=' + "joe debClass" + ' id=' + accdeb + ' ' + FieldCount + ' placeholder="NNNN" /></td><td><input type="number" class=' + "joe" + ' id=' + accCredit + ' /></td><td style="width: 4%"><img src="./img/plus.svg" class="insrt-icon button-add"><img src="./img/delete.svg" class="dlt-icon"></td></tr>';
  $('#cashTable').append(newTr);

  $(document).on('click', '.dlt-icon', function() {
    $(this).parents('tr.jsrow').first().remove();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="cashTable" class="table table-bordered table-striped" required>
  <thead>
    <tr>
      <th>A/c Code</th>
      <th>Account Name*</th>
      <th>Narration*</th>
      <th>Debit*</th>
      <th>Credit</th>
    </tr>
  </thead>
  <tbody>
    <tr id="fst_row">
      <td>
        <input type="number" id="cashacc_code" placeholder="NNNN" class="form-control" name="cashacc_code" />
      </td>
      <td>
        <select class="form-control selectsch_items" name="cashacc" id="cashacc">
          <option value="Choose and items">Choose and items</option>
          <option value="1">TDS A/c Name 1</option>
          <option value="2">TDS A/c Name 2</option>
        </select>
      </td>
      <td>
        <input type="text" id="cash_narrat" placeholder="Enter here" class="form-control" pattern="[a-zA-Z0-9-_.]{1,20}" name="cash_narrat" data-toggle="modal" data-target="#narratModal" />
      </td>
      <td>
        <input type="number" id="cashdeb" placeholder="Debit Amount" class="form-control" name="cashdeb" readonly />
      </td>
      <td>
        <input type="text" id="cashcredit" class="form-control" name="cashcredit" readonly />
      </td>
      <td class="tblBtn" style="width: 4%">
        <a href="#"><img src="./img/plus.svg" class="insrt-icon button-add"></a>
        <a href="#"><img src="./img/delete.svg" class="dlt-icon dlt-icon"></a>
      </td>
    </tr>

    <tr id="sndRow">
      <td>
        <input type="number" class="form-control" id="rowNum" name="cashaccCode" placeholder="NNNN" />
      </td>
      <td>
        <select class="form-control selectsch_items" name="cashacc_nme" id="cashacc_nme">
          <option value="#">Choose and items</option>
          <option value="1">Joe</option>
          <option value="2">Joe2</option>
        </select>
      </td>
      <td>
        <input type="text" class="form-control" id="acc_narrat" placeholder="Enter here" name="acc_narrat" data-toggle="modal" data-target="#accnarratModal" />
      </td>
      <td>
        <input type="number" class="form-control debClass" id="accdeb" placeholder="NNNNNN" name="accdeb" />
      </td>
      <td>
        <input type="number" id="accCredit" class="form-control" name="accCredit" readonly />
      </td>
      <td style="width: 4%">
        <a href="#"><img src="./img/plus.svg" id="debsum" class="insrt-icon button-add"></a>
        <a href="#"><img src="./img/delete.svg" class="dlt-icon"></a>
      </td>
    </tr>
  </tbody>
</table>

<div class="row">
  <div class="col-6">
    <div class="cashTotal">
      <p class="tableTotal">Total:</p>
    </div>
  </div>
  <div class="col-6">
    <input type="number" class="totaldeb" id="totaldbt" name="totaldbt" readonly>
  </div>
</div>

I am just beginner, any help would be grateful.

Thank you.


Solution

  • EDIT: I'm sorry Joe, it looks like I attached your fiddle to the link other than my updated copy. Please check the link out again.

    I've created a JSfiddle using yours for a working example.

    I modified your code to make it easier by adding an attribute on your debit input of data-action="sumDebit" and added in this snippet.

    $('body').on('change', '[data-action="sumDebit"]', function() { //Attach an event to body that binds to all tags that has the [data-action="sumDebit"] attribute. This will make sure all over dynamically added rows will have the trigger without us having to readd after ever new row.
        var total = 0;
      $('[data-action="sumDebit"]').each(function(i,e) { //Get all tags with [data-action="sumDebit"]
        var val = parseFloat(e.value); //Get int value from string
        if(!isNaN(val)) //Make sure input was parsable. If not, result come back as NaN
                total += val;
      });
      $('#totaldbt').val(total); //Update value to total
    });