我正在使用jQuery的DataTables插件来排序表格字段。我的问题是:如何禁用特定列的排序功能?我已经尝试了以下代码,但它没有起作用:
"aoColumns": [
{ "bSearchable": false },
null
]
我还尝试了以下代码:
"aoColumnDefs": [
{
"bSearchable": false,
"aTargets": [ 1 ]
}
]
但是这仍然没有产生期望的结果。
我正在使用jQuery的DataTables插件来排序表格字段。我的问题是:如何禁用特定列的排序功能?我已经尝试了以下代码,但它没有起作用:
"aoColumns": [
{ "bSearchable": false },
null
]
我还尝试了以下代码:
"aoColumnDefs": [
{
"bSearchable": false,
"aTargets": [ 1 ]
}
]
但是这仍然没有产生期望的结果。
这就是您正在寻找的内容:
$('#example').dataTable( {
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ 1 ] }
]
});
[-1]
,然后是 [1]
,[2]
等等吗? -1
是什么意思?在 dataTables 中,列的索引难道不是从 1
开始的吗? - Dan Nissenbaum"aoColumnDefs": [ { 'bSortable': false, 'aTargets': [0,1,6] } ],
请建议如何解决这个问题。谢谢。 - Kamleshth
上使用data-orderable="false"
。例如,下面表格中的第二列“Avatar”将无法排序:<table id="example" class="display" width="100%" data-page-length="25">
<thead>
<tr>
<th>Name</th>
<th data-orderable="false">Avatar</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td><img src="https://www.gravatar.com/avatar/8edcff60cdcca2ad650758fa524d4990?s=64&d=identicon&r=PG" alt="" style="width: 64px; height: 64px; visibility: visible;"></td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td><img src="https://www.gravatar.com/avatar/98fe9834dcca2ad650758fa524d4990?s=64&d=identicon&r=PG" alt="" style="width: 64px; height: 64px; visibility: visible;"></td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
...[ETC]...
</tbody>
</table>
请查看https://jsfiddle.net/jhfrench/6yxvxt7L/|此处的一个编程示例。
为了禁用第一列排序,请使用以下代码在datatables jquery中尝试。这里的null表示启用排序。
$('#example').dataTable( {
"aoColumns": [
{ "bSortable": false },
null,
null,
null
]
} );
我使用Datatables 1.9.4,使用以下代码禁用了第一列的排序:
/* Table initialisation */
$(document).ready(function() {
$('#rules').dataTable({
"sDom" : "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType" : "bootstrap",
"oLanguage" : {
"sLengthMenu" : "_MENU_ records per page"
},
// Disable sorting on the first column
"aoColumnDefs" : [ {
'bSortable' : false,
'aTargets' : [ 0 ]
} ]
});
});
编辑:
您可以通过在 <th>
上使用 no-sort
类来禁用排序,
并使用以下初始化代码:
// Disable sorting on the no-sort class
"aoColumnDefs" : [ {
"bSortable" : false,
"aTargets" : [ "no-sort" ]
} ]
编辑2
在这个例子中,我使用Datatables与Bootstrap一起使用,遵循一个旧的博客文章。现在有一个链接提供了有关使用Bootstrap美化Datatables的更新材料styling Datatables with bootstrap。
我所使用的方法是在thead td中添加自定义属性,并通过检查该属性值来控制排序。
因此,HTML代码将为:
<table class="datatables" cellspacing="0px" >
<thead>
<tr>
<td data-bSortable="true">Requirements</td>
<td>Test Cases</td>
<td data-bSortable="true">Automated</td>
<td>Created On</td>
<td>Automated Status</td>
<td>Tags</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<tr>
<td>
同时,JavaScript用于初始化数据表格(它将动态获取表格本身的排序信息;)
$('.datatables').each(function(){
var bFilter = true;
if($(this).hasClass('nofilter')){
bFilter = false;
}
var columnSort = new Array;
$(this).find('thead tr td').each(function(){
if($(this).attr('data-bSortable') == 'true') {
columnSort.push({ "bSortable": true });
} else {
columnSort.push({ "bSortable": false });
}
});
$(this).dataTable({
"sPaginationType": "full_numbers",
"bFilter": bFilter,
"fnDrawCallback": function( oSettings ) {
},
"aoColumns": columnSort
});
});
data-bSortable
而不是 bSortable
。bSortable
不是有效的 HTML 属性。 - James DonnellycolumnDefs
现在接受一个class。如果您想在标记中指定要禁用的列,我会说这是首选方法:
columnDefs
现在可以接受一个class。如果您想在标记中指定要禁用的列,则此方法可能更好。
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th class="datatable-nosort">Actions</th>
</tr>
</thead>
...
</table>
然后,在你的 JS 中:
$("table").DataTable({
columnDefs: [{
targets: "datatable-nosort",
orderable: false
}]
});
以下是可以用来禁用某个列的方法:
$('#tableId').dataTable({
"columns": [
{ "data": "id"},
{ "data": "sampleSortableColumn" },
{ "data": "otherSortableColumn" },
{ "data": "unsortableColumn", "orderable": false}
]
});
所以你需要做的就是在你不想排序的列上添加 "orderable": false。
$("#example").dataTable(
{
"aoColumnDefs": [{
"bSortable": false,
"aTargets": [0, 1, 2, 3, 4, 5]
}]
}
);
"bSortable": false, "aTargets": [0]
- Matthewvar table = $('#data-tables').DataTable({
columnDefs: [{
targets: [0],
orderable: false
}]
});
如果要禁用单列排序,请尝试以下示例:
<script type="text/javascript">
$(document).ready(function()
{
$("#example").dataTable({
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ 0 ] }
]
});
});
</script>
如果需要多列,可以尝试以下示例:只需添加列号即可。默认情况下,它从0开始。
<script type="text/javascript">
$(document).ready(function()
{
$("#example").dataTable({
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ 0,1,2,4,5,6] }
]
});
});
</script>
这里只有第三列
可以工作