gpt4 book ai didi

javascript - 如何禁用数据表上特定列的搜索/过滤器?

转载 作者:行者123 更新时间:2023-11-29 19:34:23 24 4
gpt4 key购买 nike

我的数据表有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com