creating custom ordering

creating custom ordering

condondcondond Posts: 6Questions: 0Answers: 0
edited November 2010 in General
Hello all!

I want to be able to manipulate my statuses(column), when I go to sort the list comes up alphnumeric. Status I have:

Active
Inactive
Part-Time

I would like to have the statuses sort to:
Active
Part-Time
Inactive

I could just add space to the active and Inactive and that would sort correctly. Would much rather do a correct ordering through the data tables script.

Thanks in advance.

D

Replies

  • anjibmananjibman Posts: 115Questions: 10Answers: 0
    What I can suggest is you can write your own custom sorting function (see examples at http://www.datatables.net/plug-ins/sorting) and type detection (see examples at http://www.datatables.net/plug-ins/type-detection).

    And while sorting you can give weight to each status and sort according to that weight.

    Anjib
  • condondcondond Posts: 6Questions: 0Answers: 0
    Thank you but I do not understand.

    This makes sense but none of the values in each of those links talks about weighted values.

    What i would like to do is create a custom ordering to make my statuses line up right for my end users.
    Order:

    Active
    Part-Time
    Inactive

    instead of alpha numeric:
    Active
    Inactive
    Part-Time

    Is there a coding example on how to weight the values?

    Thanks

    Doug
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yes indeed: http://datatables.net/plug-ins/sorting#Priority

    Allan
  • condondcondond Posts: 6Questions: 0Answers: 0
    Like this?

    [code]

    $(document).ready( function () {
    fnPriority( a )
    {
    if ( a == "Active" ) { return 1; }
    else if ( a == "Part-Time" ) { return 2; }
    else if ( a == "Inactive" ) { return 3; }
    return 4;
    }

    jQuery.fn.dataTableExt.oSort['priority-asc'] = function(a,b) {
    var x = fnPriority( a );
    var y = fnPriority( b );

    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['priority-desc'] = function(a,b) {
    var x = fnPriority( a );
    var y = fnPriority( b );

    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    TableToolsInit.sSwfPath = "../inc/datatables/media/swf/ZeroClipboard.swf";
    var oTable = $('#example2').dataTable( {
    "sPaginationType": "full_numbers",
    "sDom": 'T<"clear">lfrtip',
    "iDisplayLength": 100
    } );;
    } );


    [/code]
  • anjibmananjibman Posts: 115Questions: 10Answers: 0
    edited November 2010
    That's should work for sorting but I am guessing now we have new data type 'priority' so I think you have to write type detection for 'priority'. And your table initializations looks like:
    [code]
    var oTable = $('#example2').dataTable( {
    "sPaginationType": "full_numbers",
    "sDom": 'T<"clear">lfrtip',
    "iDisplayLength": 100,
    "aoColumns": [
    { "sType": "priority" }, //Status column
    ........................ //your other columns
    ]
    } );
    [/code]
    Correct me Allan if I am wrong.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    As anjibman states you need you use sType. Have a look at this example: http://datatables.net/examples/plug-ins/sorting_sType.html

    Allan
This discussion has been closed.