Skip to content Skip to sidebar Skip to footer

Table Total Auto-calculated When The Newly Expanded Row Is Removed Using Jquery

So my snippet gives me this error: this.closest is not a function but everything works fine in my page, so let's forget this part. My question is I have a table where the column an

Solution 1:

First you need to set all input of removed row to 0 and then recalculate every col and row total then you remove the row:

$("#dep_it_table").on('click', '.remove', function() {
      var row = $(this).closest('tr');
      row.find(".outstanding, .received, .paid").each(function(){
         $(this).val(0)
         $(this).change();
      });
      row.remove();
});

$(document).on('input change', '.outstanding, .received, .paid', updateTable);

functionupdateTable() {
  updateRow($(this).closest("tr"));
  updateCol($(this).closest("td"), $(this));
  updateTotal($(this.closest("table")));
}

functionupdateRow($row) {
  var sum = 0,
    sum2 = 0,
    sum3 = 0;
  $row.find('.outstanding, .received, .paid').each(function(i) {
    if (!isNaN(this.value) && this.value.length != 0) {
      if ($(this).hasClass('outstanding')) {
        out = $(this).val();
        sum += parseFloat(this.value);
      } elseif ($(this).hasClass('received')) {
        reci = $(this).val();
        sum2 += parseFloat(this.value);
      } elseif ($(this).hasClass('paid')) {
        paid = $(this).val()
        sum3 += parseFloat(this.value);
      }
    }
  });
  $row.find('.amtOutstanding').val(sum + sum2 + sum3);
}

functionupdateCol($col, $input) {
  var index = $col.index() + 1;
  var sum = 0;
  $col.closest('table').find('td:nth-child(' + index + ')').find('input').each(function(i) {
    if (!isNaN(this.value) && this.value.length != 0 && !$(this).attr('id')) {
      sum += parseFloat(this.value);
    }
  });

  if ($input.hasClass('outstanding')) {
    $('#sch26_outstanding').val(sum.toFixed(2));
  } elseif ($input.hasClass('received')) {
    $('#sch26_received').val(sum.toFixed(2));
  } elseif ($input.hasClass('paid')) {
    $('#sch26_paid').val(sum.toFixed(2));
  } elseif ($input.hasClass('amtOutstanding')) {
    $('#sch26_amtOutstanding').val(sum.toFixed(2));
  }

}

functionupdateSchedule26() {
  var sum = 0,
    sum2 = 0,
    sum3 = 0,
    out, reci, paid;
  $('.outstanding, .received, .paid').each(function(i) {
    if (!isNaN(this.value) && this.value.length != 0) {
      if ($(this).hasClass('outstanding')) {
        sum += parseFloat(this.value);
      } elseif ($(this).hasClass('received')) {
        sum2 += parseFloat(this.value);
      } elseif ($(this).hasClass('paid')) {
        sum3 += parseFloat(this.value);
      }
    }
  });
  var total = (parseInt(out) + parseInt(reci)) + parseInt(paid);
  $(".amtOutstanding").val(parseFloat(total).toFixed(2));
  $('#sch26_outstanding').val(sum.toFixed(2));
  $('#sch26_received').val(sum2.toFixed(2));
  $('#sch26_paid').val(sum3.toFixed(2));
}

functionupdateTotal($table) {
  var sum = 0;
  $table.find('.amtOutstanding').each(function(i) {
    if (!isNaN(this.value) && this.value.length != 0) {
        sum += parseFloat(this.value);
    }
  });
  $('#sch26_amtOutstanding').val(sum.toFixed(2))
}

functionaddMoreDepIT() {
  var new_raw = $(
    '<tr>'+
      '<td><a href="javascript:void(0);" class="remove">Remove</td>'+
      '<td><input type="number" min="0" name="" id="" class="form-control outstanding"></td>'+
      '<td><input type="number" min="0" name="" id="" class="form-control received"></td>'+
      '<td><input type="number" min="0" name="" id="" class="form-control paid"></td>'+
      '<td><input type="number" min="0" name="" id="" class="form-control amtOutstanding" readonly></td>'+
    '</tr>'
  );
  new_raw.insertBefore('#addMore');
  $("#dep_it_table").on('click', '.remove', function() {
      var row = $(this).closest('tr');
      row.find(".outstanding, .received, .paid").each(function(){
  	     $(this).val(0)
  	     $(this).change();
      });
      row.remove();
  });
}
<linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script><tableclass="table table-sm"id="dep_it_table"><thead><tr><thstyle="width:16.67%">Name</th><thstyle="width:16.67%">Outstanding</th><thstyle="width:16.67%">Received</th><thstyle="width:16.67%">Paid</th><thstyle="width:16.67%">Sub Total</th></tr></thead><tbody><tr><td>Name 1</td><td><inputtype="number"name=""id=""class="form-control outstanding"></td><td><inputtype="number"name=""id=""class="form-control received"></td><td><inputtype="number"name=""id=""class="form-control paid"></td><td><inputtype="number"name=""id=""class="form-control amtOutstanding"readonly></td></tr><tr><td>Name 2</td><td><inputtype="number"name=""id=""class="form-control outstanding"></td><td><inputtype="number"name=""id=""class="form-control received"></td><td><inputtype="number"name=""id=""class="form-control paid"></td><td><inputtype="number"name=""id=""class="form-control amtOutstanding"readonly></td></tr><trid="addMore"><td><ahref="javascript:void(0)"class="btn btn-primary"style="padding:0px;"onclick="addMoreDepIT()"><iclass="ft-plus hidden-lg-up"></i>&nbsp;Add More</a></td><td></td><td></td><td></td><td></td></tr><tr><td>Add Total</td><td><inputtype="number"name=""id="sch26_outstanding"class="form-control total_sum"readonly></td><td><inputtype="number"name=""id="sch26_received"class="form-control total_sum"readonly></td><td><inputtype="number"name=""id="sch26_paid"class="form-control total_sum"readonly></td><td><inputtype="number"name=""id="sch26_amtOutstanding"class="form-control"readonly></td></tr></tbody></table>

Post a Comment for "Table Total Auto-calculated When The Newly Expanded Row Is Removed Using Jquery"