sort classes not getting reset after disabling and enabling with aoColumnDefs, bug plus patch!

sort classes not getting reset after disabling and enabling with aoColumnDefs, bug plus patch!

rupsrups Posts: 4Questions: 0Answers: 0
edited June 2011 in Bug reports
HI,

I'm configure a table that has only the first and last two columns sortable and the number of columns is variable. I'm doing this by using aoColumnDefs to default all columns to unsortable and enabling sorting for the required columns only with the initialisation code:

"bSort": true,
"aaSorting": [[ 0, 'asc' ]],

"aoColumnDefs": [
{ "aTargets": [ 0 ], "bSortable": true },
{ "aTargets": [ -1 ], "bSortable": true },
{ "aTargets": [ -2 ], "bSortable": true },
{ "aTargets": [ '_all' ], "bSortable": false }
]

This correctly enables sorting just for the targeted columns, however the sortable columns ( 0, -2 and -1 ) that are not currently selected for sorting have their class set to 'sorting-disabled' instead of 'sorting' so the sorting icon is not shown. If you click on a column heading to sort the class is correctly set to 'sorting-asc/desc' but the original sort column has it's class reset to 'sorting-disabled' and the sort control icon disappears. I'm using v1.8.0.

I have figured out what the problem is and have a patch :) The bug appears to be in _fnColumnOptions. Beginning on line 2623 there is a set of conditionals that set the class for the column title depending on sorting options, however this is not catching the case when sorting has been re-enabled. I added an extra conditional to catch this case and it works as expected now:

/* Check that the class assignment is correct for sorting */
if ( !oCol.bSortable ||
($.inArray('asc', oCol.asSorting) == -1 && $.inArray('desc', oCol.asSorting) == -1) )
{
oCol.sSortingClass = oSettings.oClasses.sSortableNone;
oCol.sSortingClassJUI = "";
}

**vvvvvv new conditional vvvvvv**
else if ( oCol.bSortable ||
($.inArray('asc', oCol.asSorting) == -1 && $.inArray('desc', oCol.asSorting) == -1) )
{
oCol.sSortingClass = oSettings.oClasses.sSortable;
oCol.sSortingClassJUI = oSettings.oClasses.sSortJUI;
}
**^^^^^^ new conditional ^^^^^^**

else if ( $.inArray('asc', oCol.asSorting) != -1 && $.inArray('desc', oCol.asSorting) == -1 )
{
oCol.sSortingClass = oSettings.oClasses.sSortableAsc;
oCol.sSortingClassJUI = oSettings.oClasses.sSortJUIAscAllowed;
}
else if ( $.inArray('asc', oCol.asSorting) == -1 && $.inArray('desc', oCol.asSorting) != -1 )
{
oCol.sSortingClass = oSettings.oClasses.sSortableDesc;
oCol.sSortingClassJUI = oSettings.oClasses.sSortJUIDescAllowed;
}

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    I love posts like yours here! A bug and a fix in one - nice one and thanks very much for proposing a solution to the problem. It looks correct to me and I've just committed the fix in: https://github.com/DataTables/DataTables/commit/d21a8529ee6b4cc1de40da4c993eec31be051528 .

    The change will be available in the 1.8.1.dev nightly version soon.

    Regards,
    Allan
This discussion has been closed.