在数据表的每一行中搜索值

时间:2017-02-03 08:49:55

标签: ajax class datatables rows

我正在使用动态服务器端动态表,并尝试搜索生成表的每一行的第四列以获取特定值。

我想在包含特定字符串的任何行中添加一个类。

这就是我现在所拥有的:

<script type="text/javascript" language="javascript" >
    $(document).ready(function() {

        var dataTable = $('#dynamic-table').DataTable( {
            responsive: true,
            processing: true,
            serverSide: true,
            lengthMenu: [[250, 750, 1000], [250, 750, 1000]],
            order: [[ 3, "desc" ]],
            columnDefs: [ {
              targets: [ 0, 1, 2, 5 ], //"targets": 'nosort',// using class
              orderable: false
            } ],
              "columns": [
                { "searchable": true },
                { "searchable": true },
                { "searchable": true },
                { "searchable": true },
                { "searchable": true },
                { "searchable": true }
              ],
            ajax:{
                url :"php/ajax.php", // json datasource
                type: "post",  // method  , by default get
                error: function(){  // error handling
                    $(".dynamic-table-error").html("");
                    $("#dynamic-table_processing").css("display","none");

                }
            },

            "createdRow": function( row, data, dataIndex ) {                                

                // Add a class to the row
                var data = dataTable.columns( [4, 1] ).data();
                if (data[0]=='login failed'){               
                    $(row).addClass('bg-dark-red');
                }
            }

        } );


        $("table.dataTable .sorting").append('<i class="glyph-icon"></i>');
        $("table.dataTable .sorting_asc").append('<i class="glyph-icon"></i>');
        $("table.dataTable .sorting_desc").append('<i class="glyph-icon"></i>');

    } );
</script>

正如您可以看到变量data [0],获取每一行的值但保留每一行的所有值,如example1,example2,example3等,因此无法将data [0]变量与给定的字符串进行比较例如“登录失败”。

我应该使用哪种方法或代码?

谢谢

1 个答案:

答案 0 :(得分:0)

我认为drawcallback可行:

           "drawCallback": function(settings, json) {
     var data = dataTable.columns( [4, 1] ).data();
            if (data[0]=='login failed'){               
                $(row).addClass('bg-dark-red');
            }
            }

将其替换为createdRow部分。

相关问题