I’m trying to get sorting for some columns with select elements to work in my table. To the left of the columns I have a column that is hidden at initalization.
I am using the dom-select sorting plugin from DataTables.net:
$.fn.dataTableExt.afnSortData['dom-select'] = function ( oSettings, iColumn )
{
console.log(iColumn);
var aData = [];
$( 'td:eq('+iColumn+') select', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
aData.push( $(this).val() );
} );
return aData;
};
When sorting one column, the index of the column is the same both when having and not a having hidden column to the left (iColumn in above afnSortData). But the sorting is applied on the column one step to the right when the the column to the left is hidden. If it is unhidden sorting works as supposed to.
How can I make sure that the sorting is applied to the same column which I am clicking when I have a hidden column to the left?
A quick and dirty approach would be to set the iColumn value to 1 less.
Update
A clearner approach would be this route, as mentioned on the DataTables forum: