Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在数据表的特定列上禁用搜索/筛选?_Javascript_Jquery_Filter_Datatables - Fatal编程技术网

Javascript 如何在数据表的特定列上禁用搜索/筛选?

Javascript 如何在数据表的特定列上禁用搜索/筛选?,javascript,jquery,filter,datatables,Javascript,Jquery,Filter,Datatables,我的datatable有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

我的datatable有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>

$(文档).ready(函数(){
//设置-向每个页脚单元格添加文本输入
$('#示例tfoot th')。每个(函数(){
var title=$('#示例thead th').eq($(this.index()).text();
$(this.html(“”);
} );
//数据表
变量表=$(“#示例”).DataTable();
//应用搜索
table.columns().eq(0).each(函数(colIdx){
$('input',table.column(colIdx).footer()).on('keyup change',function(){
桌子
.列(colIdx)
.search(this.value)
.draw();
} );
} );
} );

您可以使用
.not
排除要添加输入文本的列。此外,还可以添加代码,以避免将事件处理程序添加到排除列中的任何输入框中(尽管如果这些单元格中不存在输入框,则无所谓):

$(文档).ready(函数(){
//设置-向每个页脚单元格添加文本输入
$(“#示例tfoot th”)。不(“:eq(2),:eq(3),:eq(4)”//排除第3、4和5列
.每个(功能){
var title=$('#示例thead th').eq($(this.index()).text();
$(this.html(“”);
} );
//数据表
变量表=$(“#示例”).DataTable();
//应用搜索
table.columns().eq(0).each(函数(colIdx){
if(colIdx==2 | | colIdx==3 | | colIdx==4)return;//不要为这些列添加事件处理程序
$('input',table.column(colIdx).footer()).on('keyup change',function(){
桌子
.列(colIdx)
.search(this.value)
.draw();
} );
} );
} );

请参阅Fiddle:

对于具有筛选功能的列,使用相同的故事。第二列过滤禁用:

$(document).ready(function() {

$('#example').DataTable( {
           initComplete: function () {
            this.api().columns().every( function () {

                var column = this;
                var some = column.index();
                if (column.index() == 1) return;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );
        }})
};
$(文档).ready(函数(){
$('#示例')。数据表({
initComplete:函数(){
this.api().columns().every(函数(){
var列=此;
var some=column.index();
if(column.index()==1)返回;
变量选择=$(“”)
.appendTo($(column.footer()).empty())
.on('change',function(){
var val=$.fn.dataTable.util.escapeRegex(
$(this.val()
);
柱
.search(val?“^”+val+“$”:“”,true,false)
.draw();
} );
column.data().unique().sort().each(函数(d,j){
选择。追加(“”+d+“”)
} );
} );
}})
};

You filter是一个输入文本?类是什么?类是什么意思?@JoaoPaulo表中的类?它只是显示。hi@n使用此javascript,我如何才能只对一列进行排序?就像第二列是我唯一要排序的东西一样。@cumberbitch是的,您可以更改
$(“#example tfoot th')。而不是(“:eq(2),:eq(3),:eq(4)”)
$(“#示例tfoot th”).eq(“1”)
指定您只需要第二列。您还可以更改
如果(colIdx==2 | | colIdx==3 | | colIdx==4)返回;
如果(colIdx!=1)返回;
hello@n非常感谢您的回复..我实际上问的是“排序”不再是搜索。它完全起作用了,但现在我想知道如何仅使用此javascript对第二列进行排序。啊,是的,我误读了。是的,你可以。你可以在创建表时使用
aoColumns
并指定
“bSortable”来指定某些列不可排序:false
用于所有其他列。这里有一个可用的提琴:hi@Ninsly..我还有一个问题。搜索/筛选框是否可能位于顶部而不是底部,或者是否可能位于底部?就像它正好位于..下面。感谢您的提示:
如果(column.index()=1)返回;
$(document).ready(function() {

$('#example').DataTable( {
           initComplete: function () {
            this.api().columns().every( function () {

                var column = this;
                var some = column.index();
                if (column.index() == 1) return;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );
        }})
};