如何禁用特定数据表列中的搜索

How to disable searching in specific DataTable columns?

本文关键字:搜索 数据表 何禁用      更新时间:2023-09-26

我成功使用了这段代码

function refreshDataTable() {
        // The table is made sortable
        $('#order_proposal_table').DataTable({
            'destroy'           : true,     // see http://datatables.net/manual/tech-notes/3#destroy - 2nd example
            'paging'            : false,
            'scrollCollapse'    : true,
            'scrollY'           : '65vh',
            'fixedHeader'       : true,
            'dom'               : 'rt',
        }); 
    }

然后我尝试在 9 列中的 2 列中启用搜索。

所以我改变了

'dom'               : 'rt',

'dom'               : 'frt',

以显示查找输入框。这有效,但它搜索每一列,但我只需要搜索 2 列。

所以我正在尝试按照这个官方指南有选择地禁用过滤,并添加columns定义

生成的代码:

function refreshDataTable() {
        // The table is made sortable
        $('#order_proposal_table').DataTable({
            'destroy'           : true,     // see http://datatables.net/manual/tech-notes/3#destroy - 2nd example
            'paging'            : false,
            'scrollCollapse'    : true,
            'scrollY'           : '65vh',
            'fixedHeader'       : true,
            'dom'               : 'frt',
            'columns'           : [         // see https://datatables.net/reference/option/columns.searchable
                { 'searchable': false },
                { 'searchable': false },
                null,   // product code 
                null,   // description 
                { 'searchable': false }
            ]
        }); 
    }

问题是我有一个来自数据表javascript的javscript错误

类型错误:列未定义

删除代码columns有效。

我做错了什么?

我使用 columnsDef 选项解决了这个问题。

以下代码禁用了对指定列的搜索。正是我想要的。

'columnDefs'        : [         // see https://datatables.net/reference/option/columns.searchable
                { 
                    'searchable'    : false, 
                    'targets'       : [0,1,4,5,6,7,8,9] 
                },
            ]

您是否尝试过为其余 4 列传递 null 而不仅仅是指定前 5 列? 所以:

'columns': [
            { 'searchable': false },
            { 'searchable': false },
            null,   
            null,
            { 'searchable': false },
            null,   
            null,   
            null,   
            null
        ]

我会将其作为评论发布,但我无法包含该示例。

请记住,"搜索"的默认值为 true,因此要打开搜索某些列并将其关闭为其他列,您需要执行以下两个选项中的一个或另一个:

1) 保留默认设置,并关闭特定列的可搜索:

"columnDefs": [
    { "searchable": false, "targets": 0,3,5 }
]

或2)关闭默认值,然后将其更改为特定列之一

"searching": false,
"columnDefs": [{
    "searchable": true, "targets": 1,2,4,6
}],

使用"searchabe":如果默认的"搜索"仍设置为 true,则特定列为 true 将不会关闭未提及的列。

我使用bSearchable false从搜索中排除了第二列

lang-js
"aoColumns": [
                null,
                { "bSearchable": false }
            ]

希望您发现此代码对您有所帮助。