【发布时间】:2019-05-21 22:52:37
【问题描述】:
我似乎无法隐藏 DataTables 的默认搜索栏。到目前为止,我已经尝试了来自this 线程的解决方案,但是设置bFilter:false 会完全禁用过滤,所以我在页脚中的搜索框不再起作用了。
我已经发了jsfiddle
我的 HTML:
<thead>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
</thead>
<tbody>
<table id="mytable">
<thead>
<tr>
<th>name</th>
<th>type</th>
<th>color</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
<tbody>
<tr>
<td>apple</td>
<td>fruit</td>
<td>red</td>
</tr>
<tr>
<td>banana</td>
<td>fruit</td>
<td>yellow</td>
</tr>
<tr>
<td>carrot</td>
<td>vegie</td>
<td>red</td>
</tr>
<tr>
<td>potato</td>
<td>vegie</td>
<td>brown</td>
</tr>
</tbody>
</table>
</tbody>
和 jQuery 代码:
$(document).ready(function(){
let table = $('#mytable').DataTable();
$('#mytable tfoot th').each( function (i) {
let title = $('#mytable thead th').eq( $(this).index() ).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" data-index="'+i+'" />' );
} );
$( table.table().container() ).on( 'keyup', 'tfoot input', function () {
table
.column( $(this).data('index') )
.search( this.value )
.draw();
} );
});
非常感谢任何帮助。
【问题讨论】:
-
我在我的帖子中提到过,提供的解决方案对我不起作用,因为它不仅会禁用搜索栏过滤,还会禁用桌面搜索框的过滤,我想保留它。
标签: javascript jquery datatables