What is the pre function used for when creating a sort by type?
What is the pre function used for when creating a sort by type?
I am trying to create my own Sort by Type similar to the enum type (http://datatables.net/plug-ins/sorting#how_to_type). I cannot get it to work unless I write the switch statement in the desc and asc functions. I have a feeling I am doing something incorrectly with the pre function, what is it used for/how do you get it work correctly?
This discussion has been closed.
Replies
In DataTables 1.10 the `-pre` method is going to be a lot more important as `-asc` and `-desc` will be decremented (still supported, but will be removed in future). See this commit for more details:
https://github.com/DataTables/DataTables/commit/6b605936f794c0ec83cd5a63a32c4f995f6254d0
So I guess the question is - how are you currently using the `-pre` method?
Allan
[code]$.extend ( $.fn.dataTableExt.oSort, { "enum-priority-pre" : function ( a )
{
var nPos;
nPos = $.inArray ( sPriority, asPrioritySort );
if ( nPos > -1 )
{
return nPos;
}
else
{
return $.inArray ( 'Routine', asPrioritySort );
}
},
"enum-priority-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"enum-priority-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
[/code]
And I have a column that is setup with "sType" : "enum-priority". The column doesn't seem to respect my custom sorting by type though, it just seems to do alphabetical as a normal string. So I changed the setup to be something like this:
[code]
function fnPrioritySort ( sPriority )
{
var nPos;
nPos = $.inArray ( sPriority, asPrioritySort );
if ( nPos > -1 )
{
return nPos;
}
else
{
return $.inArray ( 'Routine', asPrioritySort );
}
}
$.extend ( $.fn.dataTableExt.oSort, {
'enum-priority-asc' : function ( a, b )
{
return ((fnPrioritySort(a) < fnPrioritySort(b)) ? 1 : ((fnPrioritySort(a) > fnPrioritySort(b)) ? -1 : 0));
},
'enum-priority-desc' : function ( a, b )
{
return ((fnPrioritySort(a) < fnPrioritySort(b)) ? -1 : ((fnPrioritySort(a) > fnPrioritySort(b)) ? 1 : 0));
}
} );
[/code]
I know what I have given you isn't as good as a jsFiddle or direct link but I feel something is wrong with my initialization of the column. Is something else required besides sType that I am failing to see?
Thanks Allan, you're the best,
Drew
Can you run the debugger over the table? That might yield a clue.
Allan