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

标签 javascript jquery filter datatables

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

相关文章:

javascript - 使用 API 调用创建 yeoman 生成器

javascript - 如何在javascript中播放另一个声音后播放一个声音文件

javascript - 我可以在 Ember 2.1 中升级 jQuery 2 吗?

javascript - 如何使用 Ajax 发送和接收隐藏值

javascript - 定义一个变量然后调用它(未定义)

javascript - 一起记录 Firefox/jQuery/CSS 动画的错误?

javascript - 响应覆盖 div 下的链接

jquery 下拉移动菜单链接不起作用

javascript - 根据年份范围过滤对象

java - 如何跳过文本文件中的某些输入