如何禁用对一列的过滤并将其保留在 jQuery 数据表中的其他列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14879444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable filtering on one column and keep it for other columns in a jQuery Datatable?
提问by DhawalV
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.
我是 jQuery 的新手,我需要知道是否有任何方法可以禁用对 jQuery 数据表中的一列的过滤?我的数据表有 5 列,我需要禁用最后一列的过滤。
回答by Blazemonger
Use the bSearchable
flag. From the docs:
使用bSearchable
标志。从文档:
// 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
] } );
} );
回答by César León
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] }
]
});
回答by Bsienn
Here's how to disable global search filtering on multiple columns using Datatable ColumnDef.
以下是如何使用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.
这将启用对第 0、1 和 2 列索引的搜索,并禁用其余所有列。规则从上到下适用,优先。
回答by luke-sully
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.
我花了很长时间试图弄清楚这应该是一个简单的任务,所以对于任何仍在寻找的人来说,只需添加前 3 行并参考您要禁用的列,我的是第 5 列。
回答by user1322977
This also works. Just change the number 4 to your desired column number:
这也有效。只需将数字 4 更改为您想要的列号:
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('');
}
});
},
}
);