【问题标题】:Update Calculations When Table Row is Added / Removed添加/删除表行时更新计算
【发布时间】:2020-03-20 06:58:06
【问题描述】:

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

【问题讨论】:

    标签: javascript jquery jquery-calculation jquery.repeater


    【解决方案1】:

    您应该在对表格进行任何修改后调用 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>

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多