添加/删除表行时更新计算

时间:2019-11-25 01:30:41

标签: javascript jquery jquery-calculation jquery.repeater

我正在尝试使用jAutoCalc插件对数字进行一些计算 没关系,计算正确。

但是当我尝试使用jquery.repeater插件向表中添加行时, 新行不计算在内。 另外,当我删除行表格时,总金额未更新!

有什么帮助吗?

jsfiddle此处:https://jsfiddle.net/vwqe0oLk/4/

/** autoCalc **/
$(document).ready(function() {
  function autoCalcSetup() {
    $('table').jAutoCalc('destroy');
    $('table tr').jAutoCalc({
      keyEventsFire: true,
      decimalPlaces: 2,
      emptyAsZero: true
    });
    $('table').jAutoCalc({
      decimalPlaces: 2
    });
  }

  autoCalcSetup();
});

/** confirm delete row **/
$(document).ready(function() {
  $('#deleteRow').on('shown.bs.modal', function(e) {
    // store the clicked element as data on the confirm button
    $('#confirmDeleteBtn').data('triggered-from', e.relatedTarget);
  });

  $('#confirmDeleteBtn').click(function() {
    // retrieve the button that triggered the action and use it
    var trigger = $(this).data('triggered-from');

    $(trigger).closest('tr').remove();
    $('#deleteRow').modal('hide');
  });
});
<div class="repeater">
  <table class="table table-striped table-bordered table-hover mb-0" data-repeater-list="category-group">
    <thead>
      <tr>
        <th>type</th>
        <th>unit price</th>
        <th>numbers</th>
        <th>total</th>
        <th>delete</th>
      </tr>
    </thead>
    <tbody>
      <tr data-repeater-item="">
        <td>
          <input id="type" type="text" value="brand A" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="unitPrice" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="numbers" class="form-control" required="">
        </td>
        <td>
          <input type="text" calcu="total" jautocalc="{unitPrice} * {numbers}" value="" class="form-control">
        </td>
        <td class="text-center">
          <div class="action-btns">
            <a data-toggle="modal" data-target="#deleteRow" class="btn btn-icon btn-danger btn-add-Delete" href="#">X</a>
          </div>
        </td>
      </tr>
      <tr data-repeater-item="">
        <td>
          <input id="type" type="text" value="brand A" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="unitPrice" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="numbers" class="form-control" required="">
        </td>
        <td>
          <input type="text" calcu="total" jautocalc="{unitPrice} * {numbers}" value="" class="form-control">
        </td>
        <td class="text-center">
          <div class="action-btns">
            <a data-toggle="modal" data-target="#deleteRow" class="btn btn-icon btn-danger btn-add-Delete" href="#">X</a>
          </div>
        </td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="3"><strong>Total cost</strong></td>
        <td><input id="totalCost" class="totalSum" type="text" value="" jautocalc="SUM({total})"></td>
        <td></td>
      </tr>
    </tfoot>
  </table>
  <div class="form-group">
    <a data-repeater-create="" class="btn btn-success btn-addMoreField mt-10"><i class="ik ik-plus"></i>Add more</a>
  </div>
</div>









<div class="modal fade" id="deleteRow" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="deleteModalLabel">delete</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        are you sure to delete this ?
      </div>
      <div class="modal-footer">
        <button type="button" id="confirmDeleteBtn" class="btn btn-danger" data-dismiss="modal">delete</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

在表中进行任何修改后,您应该调用 autoCalcSetup()函数。

示例: https://jsfiddle.net/214umqL6/

/** autoCalc **/
$(document).ready(function() {
  function autoCalcSetup() {
    $('table').jAutoCalc('destroy');
    $('table tr').jAutoCalc({
      keyEventsFire: true,
      decimalPlaces: 2,
      emptyAsZero: true
    });
    $('table').jAutoCalc({
      decimalPlaces: 2
    });
  }

  autoCalcSetup();

/** confirm delete row **/

  $('#deleteRow').on('shown.bs.modal', function(e) {
    // store the clicked element as data on the confirm button
    $('#confirmDeleteBtn').data('triggered-from', e.relatedTarget);
  });

  $('#confirmDeleteBtn').click(function() {
    // retrieve the button that triggered the action and use it
    var trigger = $(this).data('triggered-from');
    autoCalcSetup(); // call the function

    $(trigger).closest('tr').remove();
    $('#deleteRow').modal('hide');
  });
});
<div class="repeater">
  <table class="table table-striped table-bordered table-hover mb-0" data-repeater-list="category-group">
    <thead>
      <tr>
        <th>type</th>
        <th>unit price</th>
        <th>numbers</th>
        <th>total</th>
        <th>delete</th>
      </tr>
    </thead>
    <tbody>
      <tr data-repeater-item="">
        <td>
          <input id="type" type="text" value="brand A" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="unitPrice" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="numbers" class="form-control" required="">
        </td>
        <td>
          <input type="text" calcu="total" jautocalc="{unitPrice} * {numbers}" value="" class="form-control">
        </td>
        <td class="text-center">
          <div class="action-btns">
            <a data-toggle="modal" data-target="#deleteRow" class="btn btn-icon btn-danger btn-add-Delete" href="#">X</a>
          </div>
        </td>
      </tr>
      <tr data-repeater-item="">
        <td>
          <input id="type" type="text" value="brand A" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="unitPrice" class="form-control" required="">
        </td>
        <td>
          <input type="number" value="0" id="" calcu="numbers" class="form-control" required="">
        </td>
        <td>
          <input type="text" calcu="total" jautocalc="{unitPrice} * {numbers}" value="" class="form-control">
        </td>
        <td class="text-center">
          <div class="action-btns">
            <a data-toggle="modal" data-target="#deleteRow" class="btn btn-icon btn-danger btn-add-Delete" href="#">X</a>
          </div>
        </td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td colspan="3"><strong>Total cost</strong></td>
        <td><input id="totalCost" class="totalSum" type="text" value="" jautocalc="SUM({total})"></td>
        <td></td>
      </tr>
    </tfoot>
  </table>
  <div class="form-group">
    <a data-repeater-create="" class="btn btn-success btn-addMoreField mt-10"><i class="ik ik-plus"></i>Add more</a>
  </div>
</div>









<div class="modal fade" id="deleteRow" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="deleteModalLabel">delete</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        are you sure to delete this ?
      </div>
      <div class="modal-footer">
        <button type="button" id="confirmDeleteBtn" class="btn btn-danger" data-dismiss="modal">delete</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
      </div>
    </div>
  </div>
</div>

您可以执行相同的“添加”功能