使用Jquery删除新扩展的行时,表总数自动计算

时间:2018-10-08 09:57:51

标签: javascript jquery html

所以我的代码段给了我这个错误:this.closest is not a function,但是在我的页面上一切正常,所以让我们忘记这部分。

我的问题是我有一个表,当用户输入一些值时,可以参考此https://stackoverflow.com/a/52642412/8826120注释来自动计算列和行。现在,我要添加更多按钮,并且所有功能都可以正常工作(添加新的输入值),但是当我删除新行时,它无法自动计算总值。

  

在删除新扩展的行后,如何使总行自动更新?

您可以从下面检查它,但是正如我之前仅在此页面上所说的那样,我有error:this.closest is not a function。预先感谢

注意:我将像这样的功能Remove table rows updating total data using jQuery

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

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

function updateRow($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);
      } else if ($(this).hasClass('received')) {
        reci = $(this).val();
        sum2 += parseFloat(this.value);
      } else if ($(this).hasClass('paid')) {
        paid = $(this).val()
        sum3 += parseFloat(this.value);
      }
    }
  });
  $row.find('.amtOutstanding').val(sum + sum2 + sum3);
}

function updateCol($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));
  } else if ($input.hasClass('received')) {
    $('#sch26_received').val(sum.toFixed(2));
  } else if ($input.hasClass('paid')) {
    $('#sch26_paid').val(sum.toFixed(2));
  } else if ($input.hasClass('amtOutstanding')) {
    $('#sch26_amtOutstanding').val(sum.toFixed(2));
  }

}

function updateSchedule26() {
  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);
      } else if ($(this).hasClass('received')) {
        sum2 += parseFloat(this.value);
      } else if ($(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));
}

function updateTotal($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))
}

function addMoreDepIT() {
  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() {
      $(this).closest('tr').remove();
      updateTable();
  });
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<table class="table table-sm" id="dep_it_table">
  <thead>
    <tr>
      <th style="width:16.67%">Name</th>
      <th style="width:16.67%">Outstanding</th>
      <th style="width:16.67%">Received</th>
      <th style="width:16.67%">Paid</th>
      <th style="width:16.67%">Sub Total</th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td>Name 1</td>
      <td><input type="number" name="" id="" class="form-control outstanding"></td>
      <td><input type="number" name="" id="" class="form-control received"></td>
      <td><input type="number" name="" id="" class="form-control paid"></td>
      <td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
    </tr>
    <tr>
      <td>Name 2</td>
      <td><input type="number" name="" id="" class="form-control outstanding"></td>
      <td><input type="number" name="" id="" class="form-control received"></td>
      <td><input type="number" name="" id="" class="form-control paid"></td>
      <td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
    </tr>
    
    <tr id="addMore">
      <td><a href="javascript:void(0)" class="btn btn-primary" style="padding:0px;" onclick="addMoreDepIT()"><i class="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><input type="number" name="" id="sch26_outstanding" class="form-control total_sum" readonly></td>
      <td><input type="number" name="" id="sch26_received" class="form-control total_sum" readonly></td>
      <td><input type="number" name="" id="sch26_paid" class="form-control total_sum" readonly></td>
      <td><input type="number" name="" id="sch26_amtOutstanding" class="form-control" readonly></td>
    </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

首先,您需要将所有已删除行的所有输入设置为0,然后重新计算每个列和行总数,然后删除该行:

$("#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);

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

function updateRow($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);
      } else if ($(this).hasClass('received')) {
        reci = $(this).val();
        sum2 += parseFloat(this.value);
      } else if ($(this).hasClass('paid')) {
        paid = $(this).val()
        sum3 += parseFloat(this.value);
      }
    }
  });
  $row.find('.amtOutstanding').val(sum + sum2 + sum3);
}

function updateCol($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));
  } else if ($input.hasClass('received')) {
    $('#sch26_received').val(sum.toFixed(2));
  } else if ($input.hasClass('paid')) {
    $('#sch26_paid').val(sum.toFixed(2));
  } else if ($input.hasClass('amtOutstanding')) {
    $('#sch26_amtOutstanding').val(sum.toFixed(2));
  }

}

function updateSchedule26() {
  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);
      } else if ($(this).hasClass('received')) {
        sum2 += parseFloat(this.value);
      } else if ($(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));
}

function updateTotal($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))
}

function addMoreDepIT() {
  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();
  });
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<table class="table table-sm" id="dep_it_table">
  <thead>
    <tr>
      <th style="width:16.67%">Name</th>
      <th style="width:16.67%">Outstanding</th>
      <th style="width:16.67%">Received</th>
      <th style="width:16.67%">Paid</th>
      <th style="width:16.67%">Sub Total</th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td>Name 1</td>
      <td><input type="number" name="" id="" class="form-control outstanding"></td>
      <td><input type="number" name="" id="" class="form-control received"></td>
      <td><input type="number" name="" id="" class="form-control paid"></td>
      <td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
    </tr>
    <tr>
      <td>Name 2</td>
      <td><input type="number" name="" id="" class="form-control outstanding"></td>
      <td><input type="number" name="" id="" class="form-control received"></td>
      <td><input type="number" name="" id="" class="form-control paid"></td>
      <td><input type="number" name="" id="" class="form-control amtOutstanding" readonly></td>
    </tr>
    
    <tr id="addMore">
      <td><a href="javascript:void(0)" class="btn btn-primary" style="padding:0px;" onclick="addMoreDepIT()"><i class="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><input type="number" name="" id="sch26_outstanding" class="form-control total_sum" readonly></td>
      <td><input type="number" name="" id="sch26_received" class="form-control total_sum" readonly></td>
      <td><input type="number" name="" id="sch26_paid" class="form-control total_sum" readonly></td>
      <td><input type="number" name="" id="sch26_amtOutstanding" class="form-control" readonly></td>
    </tr>
  </tbody>
</table>