Disable sorting for a particular column in jQuery DataTables

前端 未结 23 1494
礼貌的吻别
礼貌的吻别 2020-11-30 19:58

I am using the jQuery DataTables plugin to sort the table fields. My question is: how do I disable sorting for a particular column? I have tried with the following code, but

相关标签:
23条回答
  • 2020-11-30 20:34

    This is what you're looking for:

    $('#example').dataTable( {
          "aoColumnDefs": [
              { 'bSortable': false, 'aTargets': [ 1 ] }
           ]
    });
    
    0 讨论(0)
  • 2020-11-30 20:34

    Using Datatables 1.9.4 I've disabled the sorting for the first column with this code:

    /* 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 ]
            } ]
        });
    });
    

    EDIT:

    You can disable even by using the no-sort class on your <th>,

    and use this initialization code:

    // Disable sorting on the no-sort class
    "aoColumnDefs" : [ {
        "bSortable" : false,
        "aTargets" : [ "no-sort" ]
    } ]
    

    EDIT 2

    In this example I'm using Datables with Bootstrap, following an old blog post. Now there is one link with updated material about styling Datatables with bootstrap.

    0 讨论(0)
  • 2020-11-30 20:40

    As of 1.10.5, simply include

    'orderable: false'

    in columnDefs and target your column with

    'targets: [0,1]'

    Table should like like:

    var table = $('#data-tables').DataTable({
        columnDefs: [{
            targets: [0],
            orderable: false
        }]
    });
    
    0 讨论(0)
  • 2020-11-30 20:41

    If you set the FIRST column orderable property to false, you must also set the default order column otherwise it won't work since first column is the default ordering column. Example below disables sorting on first column but sets second column as default order column (remember dataTables' indexes are zero based).

    $('#example').dataTable( {
      "order": [[1, 'asc']],
      "columnDefs": [
        { "orderable": false, "targets": 0 }
      ]
    } );
    
    0 讨论(0)
  • 2020-11-30 20:42
    1. Use the following code to disable ordering on first column:

      $('#example').dataTable( {
        "columnDefs": [
          { "orderable": false, "targets": 0 }
        ]
      } );
      
    2. To disable default ordering, you can also use:

      $('#example').dataTable( {
           "ordering": false, 
      } );
      
    0 讨论(0)
  • 2020-11-30 20:43
    $("#example").dataTable(
      {
        "aoColumnDefs": [{
          "bSortable": false, 
          "aTargets": [0, 1, 2, 3, 4, 5]
        }]
      }
    );
    
    0 讨论(0)
提交回复
热议问题