【问题标题】:Jquery datatable search and sort not working with .html() functionJquery 数据表搜索和排序不适用于 .html() 函数
【发布时间】:2013-12-27 15:48:28
【问题描述】:

嗨朋友们,我在使用 .html() 函数时遇到了 dataTables 搜索和排序功能的问题...替换 div 中的内容...替换的内容来自使用此功能的 ajax 请求....

function str(gs)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var strArray = xmlhttp.responseText.split("::");
            $("#field1").html(strArray[0]);
            $("#fieldb").html(strArray[1]); //this line replaces the content of tbody where datatables search and sort doesn't work
            $("#tiger1").html(strArray[2]);
        }
    }
    xmlhttp.open("GET", "data/readv-data.php?scode="+gs, true);
    xmlhttp.send();
}

这里是html

<table cellpadding="0" cellspacing="0" border="0" class="display" rel="datatable" id="rep">
    <thead>
        <tr>
            <th>Library</th>
            <th>Book</th>
            <th>Date</th>
            <th>Location</th>
            <th>Type</th>
        </tr>
    </thead>
    <tbody id="fieldb">
    </tbody>
</table>

【问题讨论】:

  • 请提供更多信息。您是说数据根本没有出现,这意味着 .html() 似乎没有工作,或者数据出现但搜索/排序不起作用。如果您看到数据,请在 Chrome 中按 F12,转到控制台选项卡,尝试搜索/排序并告诉我您在控制台中看到的错误。 .html() 和 .text() 似乎对我有用:jsfiddle.net/MmbnK/1 尝试提醒 strArray[1] 以确保您按预期获取数据并且拆分按预期进行。
  • 数据即将到来,但可数据排序 n 搜索不起作用....
  • 无论如何你是如何使用 jQuery 的,不妨看看 jQuery ajax() 函数。

标签: javascript php jquery ajax jquery-datatables


【解决方案1】:

DataTables 有一个用于操作/替换数据的 API。当您更换标记时,您更改了标记,但 DataTables 不知道如何处理它,因为您没有告诉 DataTables 发生了更改。

$("#fieldb").html(strArray[1]);

oTable.fnDestroy();
oTable = $('#rep').dataTable();

Best Practices for replacing all table data

【讨论】:

  • 它不工作.....所以我将如何告诉数据表更改的标记.....!!!!
  • 你的解决方案后我遇到了这个错误。ReferenceError: oTable is not defined oTable.fnDestroy();
  • @user3139824 oTable 是您创建的 dataTable 对象。您没有包含这部分代码。您需要将 oTable 替换为正确的变量名称(您的问题中没有给出)。
猜你喜欢
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 2016-11-14
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2015-02-06
相关资源
最近更新 更多