【问题标题】:jQuery .load() function not working with DataTables?jQuery .load() 函数不适用于 DataTables?
【发布时间】:2019-11-03 00:37:13
【问题描述】:

我正在尝试将数据表与外部 html 表一起使用。下面的代码工作得很好:

<table id="myTable" class="table table-striped" >  
  <thead>  
    <tr>  
      <th>ENO</th>  
      <th>EMPName</th>  
      <th>Country</th>  
      <th>Salary</th>  
    </tr>  
  </thead>  
  <tbody>  
    <tr>  
      <td>001</td>  
      <td>Anusha</td>  
      <td>India</td>  
      <td>10000</td>  
    </tr>   
  </tbody>  
</table>  

但是,如果我这样做:

<div class="container">
  <div id="includedContent"></div>
</div>

以此为脚本:

$(function(){
  $("#includedContent").load("bigtableofdata.html");
});

系统不再工作。我似乎无法弄清楚这一点,我想知道是否需要以某种方式对其进行特殊格式化才能使其正常工作。非常感谢任何帮助。

【问题讨论】:

    标签: jquery html datatables


    【解决方案1】:

    你需要在load()的完整回调中初始化插件

    $("#includedContent").load("bigtableofdata.html", function(){
      /// new html exists now
      $('#myTable').DataTable(/*options*/)
    });
    

    请注意,DataTables API 中还内置了 ajax 选项配置,这是一种更常见的 ajax 加载数据的方法

    【讨论】:

    • 非常感谢,您能否指出在哪里可以找到这些 Ajax 选项?我对这一切都很陌生。谢谢。
    • 这种方法会奏效。数据表文档有您可以遵循的 ajax 示例。所有示例均显示可运行代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多