jQuery DataTable在javascript中无法使用ajax

时间:2018-03-12 06:43:45

标签: javascript ajax datatable

我正在使用jQuery数据表来显示内置响应并具有分页的表。 我正在使用javascript调用动态

的ajax

我的表看起来像

bytes
var xmlhttp = new XMLHttpRequest();
var url = '/../view_history/' + userId + '/' + networkId ;

  xmlhttp.open('GET', url, true);
  xmlhttp.send();

  xmlhttp.onreadystatechange = function() {

  if (xmlhttp.readyState === 4) {

    if (xmlhttp.status === 200) {
      var json_data = JSON.parse(xmlhttp.responseText);
                            
      var result = json_data.result;
      if (result == 'success') {
      /* Proceess using for loop data and then append that dynamic data in <tbody> */
       document.getElementById('deposit').innerHTML += sr + emaili + coini + address + '</td><td>' + txnhash.substring(0, 12) + '...' + '</td><td>' + amount + '</td><td>' + txn_fee + '</td><td>' + Net_bal + '</td><td>' + time + '</td></tr>'
     }
    }
   }
   }

并调用jquery DataTable函数,如下所示。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="example1" class="table table-bordered table-hover">
   <thead>
<tr>
  <th>#</th>
  <th>Email Id</th>
 <th>Network</th>
 <th>Type</th>
 <th>Address</th>
 <th>Transaction</th>
 <th>Amount</th>
 <th>Txn Fee</th>
 <th>Net_Bal</th>
 <th>Time</th>
  </tr>
</thead>
  <tbody id='deposit'>

 </tbody>
</table>

它显示所有记录,但它没有响应,分页,排序根本不起作用。请帮我整理一下这个案子。

1 个答案:

答案 0 :(得分:0)

在使用数据更新表格后(在API响应中)尝试调用DataTable()

var xmlhttp = new XMLHttpRequest();
var url = '/../view_history/' + userId + '/' + networkId ;

  xmlhttp.open('GET', url, true);
  xmlhttp.send();

  xmlhttp.onreadystatechange = function() {

    if (xmlhttp.readyState === 4) {

      if (xmlhttp.status === 200) {
        var json_data = JSON.parse(xmlhttp.responseText);
                            
        var result = json_data.result;
        if (result == 'success') {
          /* Proceess using for loop data and then append that dynamic data in <tbody> */
          document.getElementById('deposit').innerHTML += sr + emaili + coini + address + '</td><td>' + txnhash.substring(0, 12) + '...' + '</td><td>' + amount + '</td><td>' + txn_fee + '</td><td>' + Net_bal + '</td><td>' + time + '</td></tr>';

          // call the DataTable();
          $('#example1').DataTable();
        }
      }
    }
  }
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<table id="example1" class="table table-bordered table-hover">
   <thead>
<tr>
  <th>#</th>
  <th>Email Id</th>
 <th>Network</th>
 <th>Type</th>
 <th>Address</th>
 <th>Transaction</th>
 <th>Amount</th>
 <th>Txn Fee</th>
 <th>Net_Bal</th>
 <th>Time</th>
  </tr>
</thead>
  <tbody id='deposit'>

 </tbody>
</table>