Disable table sorting with className

Disable table sorting with className

arathaelarathael Posts: 1Questions: 0Answers: 0
edited February 2012 in DataTables 1.9
Hi there, i know it is posible to disable sorting on a per-column basis with classNames. Now, can i disable table sorting (bSort) only for matching elements (.no-sort) or something?

I was hoping something like:

[code]
$('.table-records').dataTable({
"bSort": ( $(this).hasClass('dont-sort-this-one-please') ) ? false : true,
});
[/code]

Thanks in advance.

Replies

  • dreamer79dreamer79 Posts: 14Questions: 0Answers: 0
    Hmmm. I use this approach:

    [code]

    var ocols=[];

    $('#example').find('thead').each(function(i1,v1){
    ocols= [];
    $(v1).find('th').each(function(i2,v2){
    var at=$(v2).attr('nosort'); //or just use $(v2).hasClass('nosort') in if
    if (typeof at!='undefined'&&at!=null&&at!=''&&at!='0'){
    ocols[ocols.length]= {
    "bSortable": false
    };
    } else {
    ocols[ocols.length]= null;
    }
    });

    $('#example').dataTable({aoColumns= ocols});

    [/code]
  • allanallan Posts: 63,493Questions: 1Answers: 10,470 Site admin
    I think your general approach looks absolutely fine, but the execution scope of the DataTables initialiser is not the element - thus 'this' in the context you have it will not be picking up the table element. I think you want something like this:

    [code]
    $('#example').dataTable({
    "bSort": ( $('#example').hasClass('dont-sort-this-one-please') ) ? false : true,
    });
    [/code]

    I see from your initialisation you had a multiple class selector - I presume you are trying to initialise multiple DataTables at the same time? That's absolutely fine, but they all get the same initialisation object - you would need to create a small function to build the init object on an element by element basis if you want that.

    Allan
This discussion has been closed.