datatables 成功后再次初始化 ajax 拉取数据

datatables initialize again after success ajax pull data

我正在使用 datatables,我遇到了一个自我需求,我想要一个实时 table 数据,我使用数据 tables 来满足这个需求,但是我有一个问题其中 datatables 不会在 json 拉取数据后初始化(在成功从服务器拉取 json 响应后)。下面是我的 json post 请求的代码(参考下面)

$.post("/test", { id: "1" }, function(response){
        if(response.success){
            var bbr = $("#ua_table tbody");
            bbr.html("");
            $.each(response.persons, function(index, value){
                bbr.append('<tr><td>' + value.name + '</td><td>' + value.address + '</td><td>' + value.job + '</td><td>' + value.contact + '</td></tr>');
            });

        }
    }, 'json');

我什至尝试在 table 的主体中添加附加内容(在脚本片段中,您可以看到具有 class 或 "load" 功能的按钮)并且它确实成功追加(每次你点击 class 为 "load" 的按钮,你会看到 table 的 tbody 中添加了)但是分页的东西(比如显示 1到 3 个条目中的 3 个和分页导航,例如 first、1、last)没有改变,所以这意味着 table 没有再次初始化,有什么想法、线索、建议、帮助、建议吗?

PS:数据table工具也没有用(没有复制,csv,excel,显示打印按钮),如你所见,我已经link 数据table工具脚本和样式,有什么想法吗?

$(document).ready(function(){
  $('#ua_table').DataTable({
        "pagingType": "full_numbers",
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": "//cdn.datatables.net/tabletools/2.2.4/swf/copy_csv_xls_pdf.swf"
        }
    });
  
  $(".load").click(function(){
    $("#ua_table tbody").append('<tr><td>Sample name</td>><td>Sample address</td><td>Sample Job</td><td>Sample Contact</td></tr>');
  });
  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="//cdn.datatables.net/tabletools/2.2.4/css/dataTables.tableTools.css" rel="stylesheet"/>
<script src="//cdn.datatables.net/tabletools/2.2.4/js/dataTables.tableTools.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>


<table class="table" id="ua_table">
  <thead>
    <th>Name</th>
    <th>Address</th>
    <th>Job</th>
    <th>Contact</th>
  </thead>
  <tbody>
    <tr>
      <td>Sample name 1</td>
      <td>Sample address 1</td>
      <td>Sample job 1</td>
      <td>Sample contact 1</td>
    </tr>
    <tr>
      <td>Sample name 2</td>
      <td>Sample address 2</td>
      <td>Sample job 2</td>
      <td>Sample contact 2</td>
    </tr>
    <tr>
      <td>Sample name 3</td>
      <td>Sample address 3</td>
      <td>Sample job 3</td>
      <td>Sample contact 3</td>
    </tr>
  </tbody>
 </table>

<button class="load">Load ajax</button>

您应该使用 row.add() 添加行。

answered your question 关于 TableTools 较早。 TableTools 没有工作,因为它的 JS 文件需要在 DataTables JS 文件之后加载。它在下面的示例中也不起作用,可能是由于 Flash 安全限制。您需要将此示例放在您的服务器上才能运行。

我还添加了 Bootstrap 样式,看来你还是想这样做。

有关代码和演示,请参见下面的示例。

$(document).ready(function(){
  $('#ua_table').DataTable({
        "pagingType": "full_numbers",
        "dom": 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": "//cdn.datatables.net/tabletools/2.2.4/swf/copy_csv_xls_pdf.swf"
        }
    });
  
  $(".load").click(function(){
    $('#ua_table').DataTable().row.add(['Sample name', 'Sample address', 'Sample Job', 'Sample Contact']).draw();
  });  
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<link href="//cdn.datatables.net/tabletools/2.2.4/css/dataTables.tableTools.css" rel="stylesheet"/>
<script src="//cdn.datatables.net/tabletools/2.2.4/js/dataTables.tableTools.min.js"></script>
<link href="//cdn.datatables.net/plug-ins/f2c75b7247b/integration/bootstrap/3/dataTables.bootstrap.css" rel="stylesheet"/>  

<script src="//cdn.datatables.net/plug-ins/f2c75b7247b/integration/bootstrap/3/dataTables.bootstrap.js"></script>


<table class="table table-striped table-bordered" id="ua_table">
  <thead>
    <th>Name</th>
    <th>Address</th>
    <th>Job</th>
    <th>Contact</th>
  </thead>
  <tbody>
    <tr>
      <td>Sample name 1</td>
      <td>Sample address 1</td>
      <td>Sample job 1</td>
      <td>Sample contact 1</td>
    </tr>
    <tr>
      <td>Sample name 2</td>
      <td>Sample address 2</td>
      <td>Sample job 2</td>
      <td>Sample contact 2</td>
    </tr>
    <tr>
      <td>Sample name 3</td>
      <td>Sample address 3</td>
      <td>Sample job 3</td>
      <td>Sample contact 3</td>
    </tr>
  </tbody>
 </table>

<button class="load">Load ajax</button>