使用jquery数据表禁用列排序

usm*_*man 152 javascript sorting jquery datatables

我使用jQuery datatables插件对表字段进行排序.我的问题是如何禁用特定列的排序?我尝试使用以下代码,但它不起作用:

"aoColumns": [
  { "bSearchable": false },
  null
]   
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下代码:

"aoColumnDefs": [
  {
    "bSearchable": false,
    "aTargets": [ 1 ]
  }
]
Run Code Online (Sandbox Code Playgroud)

但这仍然没有产生预期的结果.

wil*_*ahn 173

这就是你要找的东西:

$('#example').dataTable( {
      "aoColumnDefs": [
          { 'bSortable': false, 'aTargets': [ 1 ] }
       ]
});
Run Code Online (Sandbox Code Playgroud)

  • 你可以简单地做'aTargets':[1,2] (5认同)
  • 这对我有用.如果你想排序第一列'aTargets':[ - 1],第二列'aTargets':[1],第三列'aTargets':[2]依此类推. (3认同)
  • @Lasang - 你真的是说'[ - 1]`,然后是`[1]`,`[2]`等等?`-1`是什么意思?对于dataTables,列的索引是否从"1"开始? (2认同)

Jer*_*nch 84

从DataTables 1.10.5开始,现在可以使用HTML5 data-*属性定义初始化选项.

- dataTables文档:HTML5 data-*属性 - 表选项

所以,你可以使用data-orderable="false"th列的,你不想被排序.例如,下表中的第二列"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&amp;d=identicon&amp;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&amp;d=identicon&amp;r=PG" alt="" style="width: 64px; height: 64px; visibility: visible;"></td>
            <td>2011/07/25</td>
            <td>$170,750</td>
        </tr>
        ...[ETC]...
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

请参阅https://jsfiddle.net/jhfrench/6yxvxt7L/上的工作示例.

  • IMO,这是这里最好的答案,最简单,最优雅的方法 (8认同)
  • 这禁用了排序功能,但我发现(在1.10.12中)在初始化DataTable时默认情况下仍对列进行排序,这会使用升序的排序字形为列设置样式。如果您不希望这样做,则可以在不进行排序的情况下初始化数据表:$('#example')。DataTable({'order':[]}); (2认同)

Pau*_*raj 64

要使第一列排序禁用,请尝试使用datatables jquery中的以下代码.null表示此处的排序启用.

$('#example').dataTable( {
  "aoColumns": [
  { "bSortable": false },
  null,
  null,
  null
  ]
} );
Run Code Online (Sandbox Code Playgroud)

禁用jQuery数据表中列的排序


Pau*_*lgo 60

使用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 ]
        } ]
    });
});
Run Code Online (Sandbox Code Playgroud)

编辑:

您甚至可以使用no-sort您的课程禁用<th>,

并使用此初始化代码:

// Disable sorting on the no-sort class
"aoColumnDefs" : [ {
    "bSortable" : false,
    "aTargets" : [ "no-sort" ]
} ]
Run Code Online (Sandbox Code Playgroud)

编辑2

在这个例子中,我正在使用带有Bootstrap的Datables,遵循旧的博客文章.现在有一个链接,其中包含有关使用引导程序设置数据表样式的更新资料.


Bha*_*h B 27

我使用的只是在thead td中添加一个自定义属性,并通过自动检查attr值来控制排序.

所以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>
Run Code Online (Sandbox Code Playgroud)

用于初始化数据表的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
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 你应该使用`data-bSortable`而不是`bSortable`.`bSortable`不是有效的HTML属性. (3认同)

dtb*_*rne 14

columnDefs现在接受一个班级.如果您想在标记中指定要禁用的列,我会说这是首选方法:

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th class="datatable-nosort">Actions</th>
        </tr>
    </thead>
    ...
</table>
Run Code Online (Sandbox Code Playgroud)

然后,在你的JS中:

$("table").DataTable({
    columnDefs: [{
        targets: "datatable-nosort",
        orderable: false
    }]
});
Run Code Online (Sandbox Code Playgroud)


小智 10

以下是禁用某些列的禁用方法:

 $('#tableId').dataTable({           
            "columns": [
                { "data": "id"},
                { "data": "sampleSortableColumn" },
                { "data": "otherSortableColumn" },
                { "data": "unsortableColumn", "orderable": false}
           ]
});
Run Code Online (Sandbox Code Playgroud)

因此,您所要做的就是将"orderable":false添加到您不希望可排序的列中.


小智 6

$("#example").dataTable(
  {
    "aoColumnDefs": [{
      "bSortable": false, 
      "aTargets": [0, 1, 2, 3, 4, 5]
    }]
  }
);
Run Code Online (Sandbox Code Playgroud)


Sid*_*uri 5

对于单列排序禁用,请尝试以下示例:

<script type="text/javascript">                         
    $(document).ready(function() 
    {
        $("#example").dataTable({
           "aoColumnDefs": [
              { 'bSortable': false, 'aTargets': [ 0 ] }
           ]
        });
    });                                         
</script>
Run Code Online (Sandbox Code Playgroud)

对于“多列”,请尝试以下示例:您只需添加列号。默认情况下,它从0开始

<script type="text/javascript">                         
    $(document).ready(function() 
    {
        $("#example").dataTable({
           "aoColumnDefs": [
              { 'bSortable': false, 'aTargets': [ 0,1,2,4,5,6] }
           ]
        });
    });                                         
</script>  
Run Code Online (Sandbox Code Playgroud)

仅在这里Column 3有效


Mar*_*vic 5

1.10.5开始,只需包括

'orderable:false'

在columnDefs中并使用

'目标:[0,1]'

表应该像:

var table = $('#data-tables').DataTable({
    columnDefs: [{
        targets: [0],
        orderable: false
    }]
});
Run Code Online (Sandbox Code Playgroud)


Mos*_*hua 5

如果您将 FIRST 列orderable属性设置为 false,您还必须设置默认order列,否则它将不起作用,因为第一列是默认排序列。下面的示例禁用对第一列的排序,但将第二列设置为默认顺序列(记住 dataTables 的索引是从零开始的)。

$('#example').dataTable( {
  "order": [[1, 'asc']],
  "columnDefs": [
    { "orderable": false, "targets": 0 }
  ]
} );
Run Code Online (Sandbox Code Playgroud)