我的数据表有 5 列,我需要禁用对第 3、4 和最后一列的筛选。
请帮忙!!!
这是 javascript:
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
var title = $('#example thead th').eq( $(this).index() ).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
// DataTable
var table = $('#example').DataTable();
// Apply the search
table.columns().eq( 0 ).each( function ( colIdx ) {
$( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
table
.column( colIdx )
.search( this.value )
.draw();
} );
} );
} );
</script>
最佳答案
您也可以使用 .not
来排除您想要添加输入文本的列。此外,您还可以添加代码以避免向排除列中的任何输入框添加事件处理程序(尽管如果这些单元格中不存在输入框也没有关系):
$(document).ready(function() {
// Setup - add a text input to each footer cell
$('#example tfoot th').not(":eq(2),:eq(3),:eq(4)") //Exclude columns 3, 4, and 5
.each( function () {
var title = $('#example thead th').eq( $(this).index() ).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
// DataTable
var table = $('#example').DataTable();
// Apply the search
table.columns().eq( 0 ).each( function ( colIdx ) {
if (colIdx == 2 || colIdx == 3 || colIdx == 4) return; //Do not add event handlers for these columns
$( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
table
.column( colIdx )
.search( this.value )
.draw();
} );
} );
} );
参见 fiddle :http://jsfiddle.net/30phqqqg/1/
关于javascript - 如何禁用数据表上特定列的搜索/过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894660/