How to disable filtering on one column and keep it for other columns in a jQuery Datatable?
Solution 1
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
] } );
} );
Solution 2
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] }
]
});
Solution 3
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.
Solution 4
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.
Solution 5
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('');
}
});
},
}
);
DhawalV
Updated on November 20, 2020Comments
-
DhawalV over 3 years
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.
-
Bohdan Kuts over 8 yearsIn new version (1.10+) of dataTable you can use
"searchable": false
flag for column on table initialization. -
fatuhoku about 7 yearsWhat's the full syntax of searchable?