【问题标题】:External js file is not detecting html table rows added via ajax call外部 js 文件未检测到通过 ajax 调用添加的 html 表行
【发布时间】:2018-12-03 14:03:05
【问题描述】:

我正在从 SQL 数据库中检索数据并将其添加为我现有的 HTML 行。 这是我的代码:

Ajax 调用以获取 analyze_submit 上的数据提交

$(document).ready(function(e) {
    $('#analyze_submit').click(function() {

   var data = $("#analyze_options_select").val();


     $.ajax({
            url  :ajaxurl,
            type :'POST',
            data: { 'action': 'expense','analyze_options':data },
            success: function(data){
                $("#example tbody").html(data);

            }
        });
            $("#summary_head").slideDown('slow');
            $("#detailed_view_head").slideDown('slow');
            $("#analyze_output").slideDown(); 

    }); 

});

现有表代码:

<table id="example" class="grid">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Account</th>
                        <th>Category</th>
                        <th>Description</th>
                        <th>Amount</th>
                    </tr>
            </thead>

            <tbody>

            </tbody>
</table>

我在表tbody 部分中添加数据。

现在我正在尝试为最终的 HTML 表格添加排序\过滤功能。为此,我需要调用这个外部 js 文件。

https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js 

我面临的问题是,这个js文件没有检测到通过ajax调用从数据库中检索到的数据。 我尝试在按钮提交功能的末尾添加 $.getScript,但它不起作用。

$.getScript('https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js);

我是 jquery\javascript 的新手,目前被困在这里。有什么前进的指导吗?

【问题讨论】:

  • ajax 请求是异步的...
  • 您可能需要调用一些数据表函数来更新数据表表
  • 在您的示例中,您似乎没有告诉 dataTables 插件在哪里可以找到您的表,根据插件网站,您需要将其定义为 `$('#myTable').DataTable (); datatables.net

标签: javascript jquery html ajax


【解决方案1】:
 $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
          "url"  :ajaxurl,
    } );

【讨论】:

  • @giving me error "DataTables 警告:table id=example - 无法重新初始化 DataTable。有关此错误的详细信息,请参阅 datatables.net/tn/3"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
相关资源
最近更新 更多