Disable sorting for a particular column in jQuery DataTables

前端 未结 23 1495
礼貌的吻别
礼貌的吻别 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:44

    There are two ways, one is defined in html when you define table headers

    <thead>
      <th data-orderable="false"></th>
    </thead>
    

    Another way is using javascript, for example, you have table

    <table id="datatables">
        <thead>
            <tr>
                <th class="testid input">test id</th>
                <th class="testname input">test name</th>
        </thead>
    </table>
    

    then,

    $(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
            }
            ]
        }
        );
    }
    );
    
    0 讨论(0)
  • 2020-11-30 20:44

    set class "no-sort" in th of the table then add css .no-sort { pointer-events: none !important; cursor: default !important;background-image: none !important; } by this it will hide the arrow updown and disble event in the head.

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

    If you already have to hide Some columns, like I hide last name column. I just had to concatenate fname , lname , So i made query but hide that column from front end. The modifications in Disable sorting in such situation are :

        "aoColumnDefs": [
            { 'bSortable': false, 'aTargets': [ 3 ] },
            {
                "targets": [ 4 ],
                "visible": false,
                "searchable": true
            }
        ],
    

    Notice that I had Hiding functionality here

        "columnDefs": [
                {
                    "targets": [ 4 ],
                    "visible": false,
                    "searchable": true
                }
            ],
    

    Then I merged it into "aoColumnDefs"

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

    This works for me for a single column

     $('#example').dataTable( {
    "aoColumns": [
    { "bSortable": false 
     }]});
    
    0 讨论(0)
  • 2020-11-30 20:47

    Here is the answer !

    targets is the column number, it starts from 0

    $('#example').dataTable( {
      "columnDefs": [
        { "orderable": false, "targets": 0 }
      ]
    } );
    
    0 讨论(0)
  • 2020-11-30 20:49

    What I use is just add a custom attribute in thead td and control sorting by checking that attr value automatically.

    So the HTML code will be

    <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>
    

    And JavaScript for initializing datatables will be (it will dynamically get the sorting information from table iteself ;)

    $('.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
        });
    });
    
    0 讨论(0)
提交回复
热议问题