Disable sorting for a particular column in jQuery DataTables

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

    Requirements Test Cases Automated Created On Automated Status Tags Action

    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
        });
    });
    

    提交回复
    热议问题