【问题标题】:Pagination not working?? - jQuery or Javascript分页不起作用?? - jQuery 或 Javascript
【发布时间】:2019-11-05 05:14:17
【问题描述】:

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

我正在使用带有 jquery 的引导表。

我希望只显示 5 行,其余行是分页的。

这是我的小提琴

FIDDLE HERE..

动态添加行时如何分页。

这是一个示例代码。

$(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
  })
});
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.js"></script>
<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">
        <tbody>
          <tr id="fst_row">
            <td style="display: none;">
              <input type="number" id="staticLedger" placeholder='Ledger Number' for="staticLedger" name="ledgerno" class="form-control" />
            </td>
            <td style="display: none;">
              <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">
              </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></a>
            </td>
          </tr>
          <input type="button" class="add_Row adRow" id="add_Row" value="Add Row">
        </tbody>
      </table>

    </div>
  </div>
</div>

我做错了什么?

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

谢谢

【问题讨论】:

  • 您是否添加了Datatable CDN。因为没有 DATATABLE CDN 。并且第一个选择列表没有值
  • 是的!我正在使用 jQuery 1.10.20 cdn
  • 你好,检查这个答案stackoverflow.com/a/28141195/7943457你需要让数据表知道行已经改变,所以请重新渲染
  • @Piyush 但它不适合我

标签: javascript jquery


【解决方案1】:

您的代码 sn-p 有一些错误: 1.在表 th 标签小于 td ,您正在附加 2.Jquery定义前调用Datatable

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

$(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>

【讨论】:

  • 我面临两个错误妈妈,第一个是 “无法读取未定义的属性 'mData'” 第二个是 “无法读取属性 'parentNode' of null”
  • "无法读取未定义的属性 'mData'" -- 由于标签与附加 td 不同。计算你的 th 和 td 标签使它们等于 count.refere this -- jsfiddle.net/AishwaryaDomde/hvu5kwbo/7/#save 另外“DataTables 不支持表体中的 rowspan / colspan”
  • 妈妈,这是我目前的问题.. 请检查一下 -jsfiddle.net/gz3jbcyf/2
  • 你能在 jsbin 中分享你的代码吗?JSfilddle 显示网关错误
猜你喜欢
  • 2022-06-10
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多