分页不起作用? -jQuery或Javascript

我只是使用分页创建动态表,但是分页不适用于该表。

我将引导表与jquery一起使用。

我只允许显示5行,其余行为分页。

这是我的小提琴

FIDDLE HERE..

  

动态添加行时如何分页。

这是示例代码。

object RealmManager {
    fun getMemo() : RealmResults<Memo> {
        Realm.getDefaultInstance().use { realm ->
            return realm.where(Memo::class.java).findAll()
        }
    }

// more codes ~
}
$(document).ready(function() {
  $("#add_Row").on("click",function() {
    var counter = 0;
    var idVal = $('#tab_logic tr:last').find('td:first').html();
    var matches = idVal.match(/\d+/g);
    if (matches != null) {
      counter = Number(matches) + counter + 1;
    }
    // alert(counter);
    var newRow = $("<tr>");
    var cols = "";
    cols += '<td class="cashpay_led" style="display:none;"><input type="number" class="form-control" id="table_ledger' + counter + '" name="ledgerno" placeholder="ledger number"/></td>';
    cols += '<td><input type="number" class="form-control sel_text" id="cashacctcode' + counter + '" name="acctcode" placeholder="account code"/></td>';
    cols += '<td><select class="form-control sel_sel status" id="accountName' + counter + '" name="accountName"><option>Choose A/c Name</option></select></td>'
    cols += '<td><textarea class="form-control pname required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" maxlength="200"/></textarea></td>';
    cols += '<td><input type="text" class="form-control" id="cashdeb' + counter + '" data-action="sumDebit" name="debit" placeholder="Debit amount"/></td>';
    cols += '<td><input type="number" class="form-control comment" id="crditCash' + counter + '" data-action="sumCredit" name="credit" placeholder="Credit amount" tabindex="-1" readonly/></td>';

    cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';

    newRow.append(cols);

    $("table.order-list").append(newRow);
    setValCashVal('accountName'.concat(counter));
    bindScript();
    counter++;
  });

});

// pagination

$(function() {
  $('#tab_logic').DataTable({
    'paging': true,'lengthChange': false,'searching': true,'ordering': true,'info': false,'autoWidth': false,'pageLength': 5
  })
});

我在做什么错了?

  

如何在动态添加行中设置分页。

谢谢

mdd000000 回答:分页不起作用? -jQuery或Javascript

您的代码段中存在一些错误: 1.表中的标签小于td,您要附加 2.在jQuery定义之前调用Datatable

如果要在动态添加行中设置分页,则必须销毁它并重新初始化。 我删除了一些隐藏的属性只是为了检查,您可以将其隐藏以添加类 “ columnDefs”:[{“ class”:“ hide”,“ targets”:3}] 要么 “ columnDefs”:[{“ targets”:[2],“ visible”:false}] 希望对您有帮助

$(document).ready(function() {
  $("#add_Row").on("click",function() {
  
    var counter = 0,setValCashVal =0;
    var idVal = $('#tab_logic tr:last').find('td:first').html();
    var matches = idVal.match(/\d+/g);
    if (matches != null) {
      counter = Number(matches) + counter + 1;
    }
    // alert(counter);
    if ($.fn.DataTable.isDataTable('#tab_logic')) {
 $('#tab_logic').DataTable().destroy()}
    var newRow = $("<tr>");
    var cols = "";
    cols += '<td class="cashpay_led" ><input type="number" class="form-control" id="table_ledger' + counter + '" name="ledgerno" placeholder="ledger number"/></td>';
    cols += '<td> <input type="text" id="tdsrow' + counter + '" placeholder="Ledger Number" name="tdsrow" value="Y" th:value="Y" class="form-control" /></td>';
    
    
    cols += '<td><input type="number" class="form-control sel_text" id="cashAcctcode' + counter + '" name="acctcode" placeholder="Account code"/></td>';
    cols += '<td><select class="form-control sel_sel status" id="accountName' + counter + '" name="accountName"><option>Choose A/c Name</option></select></td>'
    cols += '<td><textarea class="form-control pname required price" name="narr" placeholder="Enter your text here" id="acc_narrat' + counter + '" maxlength="200"/></textarea></td>';  
    cols += '<td><input type="text" class="form-control" id="cashdeb' + counter + '" data-action="sumDebit" name="debit" placeholder="Debit amount"/></td>';
    cols += '<td><input type="number" class="form-control comment" id="crditCash' + counter + '" data-action="sumCredit" name="credit" placeholder="Credit amount" tabindex="-1" readonly/></td>';

    cols += '<td><button type="button" class="adRow ibtnDel" style="width:70%;">x</button></a></td>';

    newRow.append(cols);

    $("table.order-list").append(newRow);
 //   setValCashVal('accountName'.concat(counter));
    //bindScript();
   $('#tab_logic').DataTable({
    'paging': true,'lengthChange': false,'searching': true,'ordering': true,'info': false,'autoWidth': false,'pageLength': 5
  })
    counter++;
  
  
   });
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

  <head>
    <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script src="
https://cdn.datatables.net/1.10.20/js/dataTables.jqueryui.min.js"></script>
    <link  rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
    
       <link  rel="stylesheet" href=" https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link  rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.jqueryui.min.css">
    <link  rel="stylesheet" href="https://cdn.datatables.net/scroller/2.0.1/css/scroller.jqueryui.min.css">
  </head>

  <body>
  <div class="container">
  <div class="row clearfix">
    <div class="col-md-12 column">

      <table class="table table-bordered table-hover order-list" id="tab_logic">
        <thead>
          <tr style="background-color: #680779; color: #fff;">
            <th></th><th></th>
            <th class="text-center">
              Account Code
            </th>
            <th class="text-center">
              A/c Name*
            </th>
            <th class="text-center">
              Narration*
            </th>
            <th class="text-center">
              Debit*
            </th>
            <th class="text-center">
              Credit
            </th>
            <th class="text-center">
              Action
            </th>
          </tr>
        </thead>
        <tbody>
          <tr id="fst_row">
            <td >
              <input type="number" id="staticLedger" placeholder='Ledger Number' for="staticLedger" name="ledgerno" class="form-control" />
            </td>
            <td >
              <input type="text" id="tdsrow" placeholder='Ledger Number' name="tdsrow" value="Y" th:value="Y" class="form-control" />
            </td>
            <td>
              <input type="number" id="payacc_code" placeholder='Enter A/c code' for="acctcode" name="acctcode" class="form-control sel_text" />
            </td>
            <td>
              <select class="form-control sel_sel" id="payacc" name="actname" for="actname"><option>Choose A/c Name</option>
              </select>
            </td>
            <td>
              <textarea class="form-control pname required" id="pay_narrat" name="narr" for="narr" placeholder="Enter your text here"></textarea>

            </td>
            <td>
              <input type="text" id="paydeb" name="debit" for="debit" placeholder='Debit Amount' data-action="sumDebit" class="form-control" readonly />
            </td>
            <td>
              <input type="number" id="paycredit" name="credit" for="credit" placeholder='Credit Amount' data-action="sumCredit" class="form-control" tabindex="-1" readonly />
            </td>
            <td><button type="button" class="adRow" style="width:70%; cursor: not-allowed;">x</button>
            </td>
          </tr>
          <input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
        </tbody>
      </table>

    </div>
  </div>
  </div></body></html>

本文链接:https://www.f2er.com/3162138.html

大家都在问