Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable searching in specific DataTable columns?

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?

like image 589
realtebo Avatar asked Nov 30 '22 16:11

realtebo


2 Answers

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] 
                },
            ]
like image 109
realtebo Avatar answered Dec 04 '22 12:12

realtebo


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.

like image 29
DoctorMick Avatar answered Dec 04 '22 14:12

DoctorMick