最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable searching in specific DataTable columns? - Stack Overflow

programmeradmin1浏览0评论

I'm using successfully this code

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

Then I'm trying to enable searching in 2 of the 9 columns.

So I changed

'dom'               : 'rt',

into

'dom'               : 'frt',

To show the find input box. This works, but it searches trough every columns, but I need to search only into 2 columns.

So I'm trying to follow this official guide to disable filtering selectively, and add columns definition

Resulting code:

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

The problem is that I've a javscript error from the datatable javascript

TypeError: col is undefined

Removing columns the code works.

What am I doing wrong?

I'm using successfully this code

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',
        }); 
    }

Then I'm trying to enable searching in 2 of the 9 columns.

So I changed

'dom'               : 'rt',

into

'dom'               : 'frt',

To show the find input box. This works, but it searches trough every columns, but I need to search only into 2 columns.

So I'm trying to follow this official guide to disable filtering selectively, and add columns definition

Resulting code:

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 }
            ]
        }); 
    }

The problem is that I've a javscript error from the datatable javascript

TypeError: col is undefined

Removing columns the code works.

What am I doing wrong?

Share Improve this question edited Feb 9, 2016 at 16:08 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked Feb 9, 2016 at 15:57 realteborealtebo 25.6k45 gold badges125 silver badges215 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 15

I resolved using the columnsDef option.

The following code disabled search for the specified columns. Exactly what I wanted.

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

Have you tried passing null for the remaining 4 columns rather than just specifying the first 5? So:

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

I would have posted this as a comment but I couldn't include the example.

Remember that the default for 'searching' is true, so to turn on searching for some columns and turn it off for others ou need to do one or the other of these two options:

1) keep the default setting, and turn off searchable for specific columns:

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

or 2) turn off the default and then turn it one for specific columns

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

Using "searchabe": true for specific columns will NOT turn off the non-mentioned columns if the default 'searching' is still set to true.

I excluded the second column from search by using the bSearchable false

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

Hope you found this code helpful.

发布评论

评论列表(0)

  1. 暂无评论