Нумерация страниц не работает ?? - JQuery или Javascript - PullRequest
0 голосов
/ 05 ноября 2019

Я просто создаю динамическую таблицу с разбиением на страницы, но разбиение на страницы не работает с этой таблицей.

Я использую таблицу начальной загрузки с jquery.

Я хочу разрешить отображение только 5 строк, оставшиесяСтроки разбиты на страницы.

Вот моя скрипка

FIDDLE ЗДЕСЬ ..

Как разбивать на страницы при динамическомдобавить строки.

Вот пример кода.

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

Что я делаю не так?

Как установить paginate в динамически добавляемых строках.

Спасибо

1 Ответ

1 голос
/ 05 ноября 2019

В вашем фрагменте кода есть некоторая ошибка: 1. В таблице th теги меньше чем td, к которому вы добавляете 2.Calling Datatable перед определением Jquery

Если вы хотите установить paginate в динамически добавляемых строкахтогда вы должны уничтожить его и заново инициализировать. Я удалил некоторые скрытые свойства, чтобы проверить, вы можете скрыть их, добавив класс "columnDefs": [{"class": "hide", "target": 3}] ИЛИ "columnDefs": [{"target": [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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...