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?

dwaddelldwaddell Posts: 22Questions: 0Answers: 0
edited October 2012 in General
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?

Replies

  • allanallan Posts: 64,756Questions: 1Answers: 10,714 Site admin
    edited October 2012
    Good question - the `-pre` function is used to pre-format the data that is going to be sorted. The basic idea is that rather than having the sorting function manipulate the data to be sorted multiple times (two parameters are passed into the sort function, that can both be manipulated, and that might occur many times for a single item), the `-pre` function will format the data into what will be sorted (stripping HTML, grabbing an attribute etc) only onces for every item to be sorted. This results in a performance improvement.

    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
  • dwaddelldwaddell Posts: 22Questions: 0Answers: 0
    Well I had something like this:

    [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
  • allanallan Posts: 64,756Questions: 1Answers: 10,714 Site admin
    The basic idea looks okay, as long as the numbers being returned are what you expect, and they look fairly fool proof. You are using DataTables 1.9 or newer are you? The `-pre` method won't have any effect before that.

    Can you run the debugger over the table? That might yield a clue.

    Allan
  • dwaddelldwaddell Posts: 22Questions: 0Answers: 0
    edited October 2012
    Well that is the problem, thank Allan. I am using DataTables 1.8.2. I'll consider upgrading shortly.
This discussion has been closed.