为什么手动搜索后不重新绘制数据表?

Why won't DataTable redraw after manual search

本文关键字:绘制 数据表 新绘制 搜索 为什么      更新时间:2023-09-26

我有一个表,我想手动搜索。在查看了API之后,我发现我可以这样做:

table.search(somestring).draw();

,这应该搜索表并重新绘制它,或者我认为。

$(document).ready(function(){
  var table = $('table').DataTable({
    searching: false,
    paging: false,
    info: false
  }); 
  // add a bunch of rows
  for(var i=0; i<20; i++){
      table.row.add([
      "test "+i, "best "+i, "rest "+i
    ]);
  }
  // draw the table
  table.draw();
  // why won't table redraw with only rows containing 15?
  table.search('15').draw(); 
  // this doesn't work either
  /*
  $('input').on( 'keyup click', function () {
        filterColumn( 1 ); // filter only first column
    } );
  */
});
function filterColumn ( i ) {
  console.log( $('input').val() );
  $('table').DataTable().column( i ).search(
      $('input').val()
  ).draw();  
}
HTML:

<input>
<table>
  <thead>
    <tr>
      <th>column 1</th>
      <th>column 2</th>
      <th>column 3</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

Plunker: http://plnkr.co/edit/BlwWWw7mlpde4vKqoX2q?p=preview

您禁用了searching: false的搜索功能,详细信息请参见搜索选项。

为了使search()方法工作,您需要启用searching: true或通过删除此选项来启用搜索。

如果你只是想隐藏过滤控制,使用dom: 'lrtip',参见dom选项了解更多细节。