0

我正在尝试使用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>

4

1 回答 1

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>

你可以用“添加”功能做同样的事情

于 2020-02-01T10:43:51.480 回答