DhawalV
DhawalV

Reputation: 188

How to disable filtering on one column and keep it for other columns in a jQuery Datatable?

I am new to jQuery and I need to know if there's any way to disable filtering for one of the columns in my jQuery datatable? My datatable has 5 columns and I need to disable filtering for the last column.

Upvotes: 11

Views: 34201

Answers (5)

user1322977
user1322977

Reputation: 151

This also works. Just change the number 4 to your desired column number:

    var table = $('#mytable').DataTable(
        {
            initComplete: function () {
                this.api().columns().every(function () {
                    var column = this;
                    if (column[0][0] == 4) {
                        console.log(column);
                        $(column.footer()).html('');
                    }
                });
            },
        }
    );

Upvotes: 1

Abdul Rehman
Abdul Rehman

Reputation: 1794

Here's how to disable global search filtering on multiple columns using Datatable ColumnDef.

var datatable = $('#datatable').DataTable({
    "deferRender": true,

    "columnDefs": [ 
        { targets: 0, searchable: true },
        { targets: [1,2], searchable: true },
        { targets: '_all', searchable: false }
    ]
});

This will enable search on column 0, 1 & 2 index wise and disable on rest of them all. The rules apply from top to bottom taking priority.

Upvotes: 2

César León
César León

Reputation: 3050

Also you can do it like this:

    $('#ProductsTable').dataTable({
        "lengthMenu": [[20, 50, -1], [20, 50, "All"]],
        "pageLength": 20,
        "columnDefs": [
          { "orderable": false, "targets": [-1, 1] },
          { "searchable": false, "targets": [-1, 1] }
        ]
    });

Upvotes: 4

luke-sully
luke-sully

Reputation: 323

 var mSortingString = [];
var disableSortingColumn = 4; 
mSortingString.push({ "bSortable": false, "aTargets": [disableSortingColumn] });



    $(document).ready(function () {
        var table = $('#table').dataTable({
            "paging": false,
            "ordering": true,
            "info": false,
            "aaSorting": [],
            "orderMulti": true,
            "aoColumnDefs": mSortingString

        });
     });

I spent ages trying to figure this out which should have been a simple task so for anyone still looking just add in the top 3 lines and reference which column you want to disable, mine being column 5.

Upvotes: 2

Blazemonger
Blazemonger

Reputation: 92893

Use the bSearchable flag. From the docs:

// Using aoColumnDefs
$(document).ready( function() {
  $('#example').dataTable( {
    "aoColumnDefs": [
      { "bSearchable": false, "aTargets": [ 0 ] }
    ] } );
} );


// Using aoColumns
$(document).ready( function() {
  $('#example').dataTable( {
    "aoColumns": [
      { "bSearchable": false },
      null,
      null,
      null,
      null
    ] } );
} );

Upvotes: 21

Related Questions