2016-02-07 59 views
2

我尝试在我的表中查找字符串并将类添加到此行。但是这个代码不起作用。没有任何反应。这里是我的代码,后者我打电话myTable()功能:Datatables查找字符串并添加类

function myTable() { 
    var selectDateVar = $('#selectDate').val(); 

     var table = $('#example').DataTable({ // Таблица 
      "processing": true, 
      "serverSide": true, 
      "deferRender": true, 
      "bDestroy": true, 

      "sAjaxSource": "server_processing.php?data=30/09/2015", 
      "order": [[ 2, "desc" ]], 
      initComplete: function(){ 
       var api = this.api(); 

       new $.fn.dataTable.Buttons(api, { 
        buttons: [ 
         { 
         extend: 'print', 
         text: 'Принтиране', 
         'className': 'btn-lg btn btn-warning printBTN', 
         }, 
        ] 
       }); 

       api.buttons().container().appendTo('.printButton'); 
      } 

     }); 

     var indexes = table.rows().eq(0).filter(function (rowIdx) { 
      return table.cell(rowIdx, 3).data() === '180' ? true : false; 
     }); 
     table.rows(indexes).nodes().to$().addClass('highlight'); 
    } 

我的表:

enter image description here

我用这个例子https://datatables.net/reference/type/row-selector

+0

@Spirit是的,我敢肯定。 – diank

回答

2
function myTable() { 
    var selectDateVar = $('#selectDate').val(); 

    var table = $('#example').DataTable({ // Таблица 
     "processing": true, 
     "serverSide": true, 
     "deferRender": true, 
     "bDestroy": true, 

     "sAjaxSource": "server_processing.php?data=30/09/2015", 
     "order": [[ 2, "desc" ]], 
     initComplete: function(){ 
      var api = this.api(); 
      new $.fn.dataTable.Buttons(api, { 
       buttons: [ 
        { 
        extend: 'print', 
        text: 'Принтиране', 
        'className': 'btn-lg btn btn-warning printBTN', 
        }, 
       ] 
      }); 

      api.buttons().container().appendTo('.printButton'); 

      //filtering code should be inside of initComplete function 
      //but in your case an empty table is filtered 
      var indexes = table.rows().eq(0).filter(function (rowIdx) { 
       return table.cell(rowIdx, 3).data() === '180' ? true : false; 
      }); 

      table.rows(indexes).nodes().to$().addClass('highlight'); 
     } 

    }); 
} 

你需要调用数据的代码加载。目前您在表格填充服务器数据之前调用它。只需将您的代码示例添加到initComplete函数。 initComplete将在加载Ajax数据之后调用。

回答第二个问题:如果需要跨多个列搜索只需添加以下代码:

var indexes = table.rows().eq(0).filter(function (rowIdx) { 
    return table.cell(rowIdx, 3).data() === '180' && 
      table.cell(rowIdx, 0).data() === '521' ? true : false; 
}); 
+0

看到我的评论,这是我的代码。 – diank

+0

是的工作就像一个魅力,但如果我使用搜索,分页,排序,刷新表类刚刚走了。有没有解决这个问题? – diank

+2

@diank - 您可以在[** drawCallback **](https://datatables.net/reference/option/drawCallback)中执行代码,以便每次重绘表格时添加该类。 – davidkonrad