我正在使用jQuery的DataTables插件来排序表格字段。我的问题是:如何禁用特定列的排序功能?我已经尝试了以下代码,但它没有起作用:
"aoColumns": [
{ "bSearchable": false },
null
]
我还尝试了以下代码:
"aoColumnDefs": [
{
"bSearchable": false,
"aTargets": [ 1 ]
}
]
但是这仍然没有产生期望的结果。
我正在使用jQuery的DataTables插件来排序表格字段。我的问题是:如何禁用特定列的排序功能?我已经尝试了以下代码,但它没有起作用:
"aoColumns": [
{ "bSearchable": false },
null
]
我还尝试了以下代码:
"aoColumnDefs": [
{
"bSearchable": false,
"aTargets": [ 1 ]
}
]
但是这仍然没有产生期望的结果。
orderable
属性设置为false,则必须设置默认的order
列,否则它将无法正常工作,因为第一列是默认排序列。下面的示例禁用了第一列的排序,但将第二列设置为默认的排序列(请记住,dataTables的索引是从零开始的)。$('#example').dataTable( {
"order": [[1, 'asc']],
"columnDefs": [
{ "orderable": false, "targets": 0 }
]
} );
<thead>
<tr>
<td data-sortable="false">Requirements</td>
<td data-sortable="false">Automated</td>
<td>Created On</td>
</tr>
</thead>
<tbody>
<tr>
<td>
data-orderable
...请参见 https://dev59.com/7m865IYBdhLWcg3wM7u0#32281113 。data-sortable
也可以使用,但我找不到它在哪里有文档记录。 - Jeromy French"aoColumnDefs" : [
{
'bSortable' : false,
'aTargets' : [ 0 ]
}]
这里的0
表示列的索引,如果你想让多列不排序,请列出列索引值,以逗号(,
)分隔。
使用类:
<table class="table table-datatable table-bordered" id="tableID">
<thead>
<tr>
<th class="nosort"><input type="checkbox" id="checkAllreInvitation" /></th>
<th class="sort-alpha">Employee name</th>
<th class="sort-alpha">Send Date</th>
<th class="sort-alpha">Sender</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="userUid[]" value="{user.uid}" id="checkAllreInvitation" class="checkItemre validate[required]" /></td>
<td>Alexander Schwartz</td>
<td>27.12.2015</td>
<td>dummy@email.com</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#tableID').DataTable({
'iDisplayLength':100,
"aaSorting": [[ 0, "asc" ]],
'aoColumnDefs': [{
'bSortable': false,
'aTargets': ['nosort']
}]
});
});
</script>
$('#example').dataTable( {
"aoColumns": [
{ "bSortable": false
}]});
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ 3 ] },
{
"targets": [ 4 ],
"visible": false,
"searchable": true
}
],
"columnDefs": [
{
"targets": [ 4 ],
"visible": false,
"searchable": true
}
],
"aoColumnDefs"
中。有两种方法,一种是在定义表头时在HTML中定义。
<thead>
<th data-orderable="false"></th>
</thead>
另一种方法是使用JavaScript,例如,您有一个表格
<table id="datatables">
<thead>
<tr>
<th class="testid input">test id</th>
<th class="testname input">test name</th>
</thead>
</table>
$(document).ready(function() {
$('#datatables').DataTable( {
"columnDefs": [ {
"targets": [ 0], // 0 indicates the first column you define in <thead>
"searchable": false
}
, {
// you can also use name to get the target column
"targets": 'testid', // name is the class you define in <th>
"searchable": false
}
]
}
);
}
);
Use the following code to disable ordering on first column:
$('#example').dataTable( {
"columnDefs": [
{ "orderable": false, "targets": 0 }
]
} );
To disable default ordering, you can also use:
$('#example').dataTable( {
"ordering": false,
} );
代码将会长成这个样子:
$(".data-cash").each(function (index) {
$(this).dataTable({
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page",
"oPaginate": {
"sPrevious": "Prev",
"sNext": "Next"
}
},
"bSort": false,
"aaSorting": []
});
});
你也可以像这样使用负索引:
$('.datatable').dataTable({
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap",
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ -1 ] }
]
});