Jquery 数据表搜索、排序不起作用

Jquery 数据表搜索、排序不起作用,jquery,css,search,datatables,Jquery,Css,Search,Datatables,我在代码中使用Datatable,但搜索、排序和分页不起作用 有一个静态函数,我调用它。数据已成功显示在表中,但当我搜索任何数据时,搜索不起作用。这将显示“无匹配记录”,其中我输入的文本存在于表中 检查GIF图像 这就是我所尝试的 <table id="example" class="display nowrap cell-border" style="width:100%;" cellspacing="0"> </table> <script typ

我在代码中使用Datatable,但搜索、排序和分页不起作用 有一个静态函数,我调用它。数据已成功显示在表中,但当我搜索任何数据时,搜索不起作用。这将显示“无匹配记录”,其中我输入的文本存在于表中

检查GIF图像

这就是我所尝试的

<table id="example"   class="display nowrap cell-border" style="width:100%;" cellspacing="0">
  </table>


<script type="text/javascript">
       success: function (result) {
                $("#example").empty()
                if (re.length > 0) {
                    $("#example").append
                    ("<thead><tr><th>Service Type</th><th>Service frequency</th><th>Last performed</th><th>Next Service</th><th>Create reminder</th></tr></thead>");
                    for (var i = 0; i < re.length; i++) {
                        if (re[i] !== null) {
                            $("#example").append("<tbody><tr><td>" +
                                re[i][0] + "</td><td>" +
                                re[i][1] + "</td><td>" +
                                re[i][2] + "</td><td>" +
                                re[i][3] + "</td><td>" +
                                re[i][4] + "</td></tr></tbody>");

                            sdate = re[i][2];
                        }      
                             }
                }
                var myTable = $('#example').DataTable();
            },

    </script>

成功:功能(结果){
$(“#示例”).empty()
如果(re.length>0){
$(“#示例”).append
(“服务类型服务频率上次执行下次服务创建提醒”);
对于(变量i=0;i
链接



检查gif图像

请使用我用测试数据创建的以下代码进行尝试

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
           <tr>
           <th>Service Type</th>
           <th>Service frequency</th>       
           </tr>
        </thead>
        <tbody id="tabledata">      
        </tbody>
    </table>

    $(document).ready(function() {
        var myTable = $('#example').DataTable();
        success: function(result) {
                $("#tabledata").empty()
                if (re.length > 0) {
                    for (var i = 0; i < re.length; i++) {
                        if (re[i] !== null) {

                            myTable.row.add([re[i][0], re[i][1], re[i][2], re[i][3], re[i][4]]).draw();

                            sdate = re[i][2];
                        }
                    }
                }

            },

    });

服务类型
服务频率
$(文档).ready(函数(){
var myTable=$(“#示例”).DataTable();
成功:功能(结果){
$(“#tabledata”).empty()
如果(re.length>0){
对于(变量i=0;i
我这样做

 $("#example").empty()

                if (re.length > 0) {


                    $('#example thead').append(
                    "<tr><th>Service Type</th><th>Service frequency</th><th>Last performed</th><th>Next Service</th><th>Create reminder</th></tr>"
                       );

                    for (var i = 0; i < re.length; i++) {
                        if (re[i] !== null) {
                            $('#example tbody').append('<tr><td>' + re[i][0] + '</td><td>' + re[i][1] + '</td><td>' + re[i][2] + '</td><td>' + re[i][3] + '</td><td>' + re[i][4] + '</td></tr>');
                        }
                    }
                }

<table id="example"   class="display nowrap cell-border" style="width:100%;" cellspacing="0">
                     <thead></thead>
                <tbody></tbody>
                 </table>
$(“#示例”).empty()
如果(re.length>0){
$('#示例thead')。追加(
“服务类型服务频率上次执行下次服务创建提醒”
);
对于(变量i=0;i
这对我来说很有效,因为我在发布有问题的代码时生成了错误的表

$('#example ').append("<thead><tr><th></th></tr></thead>
$('#示例')。追加(“
我像这样重新编码

 $('#example thead').append(tr><th></th></tr>);
$('#示例thead')。追加(tr>);
我也是这么做的
这对我来说非常适合

我知道这是一篇老文章,但我今天用一个 非常简单的解决方案。我在DataTables论坛上找到了答案


基本上,我是在foreach循环中显示数据的,就像上面链接中的海报一样,我在每个循环迭代中重复我的
块,防止数据表按预期运行。(这也将允许显示灰色/白色条带。)

使用myTable.draw();where?@VenkataKrishnaReddyafter var myTable=$(“#示例”).DataTable();我尝试但不工作查看您的代码,似乎每行都有一个
。这是无效的标记,可能是您出现问题的原因。我不想在单击按钮时添加行。我只显示数据,我已完成此显示数据,但搜索不工作。我不想在单击按钮时添加行。我仅显示数据,我已完成此显示数据,但搜索不起作用欢迎使用堆栈溢出!请添加一些解释,说明此代码为何有助于OP。这将有助于提供未来观众可以从中学习的答案。有关更多信息,请参阅。这解决了我的问题。我也在做同样的事情
 $('#example thead').append(tr><th></th></tr>);