Help how to use sorting plugins

Help how to use sorting plugins

rasmuslindstromrasmuslindstrom Posts: 7Questions: 0Answers: 0
edited September 2012 in General
I have a table where one column has a time format in hours and minutes like this: 3h 24m

I would like to sort on this column, but I can't figure out how, the best thing would probably be to replace "h " with a ".", remove the "m" and then sort it as 3.24

It is the 3rd of 5 in the row, and my code this far looks like this, could someone please help me to get this code to work.

[code]$(document).ready( function() {

$('#example').dataTable( {
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": true,
"bInfo": true,
"bAutoWidth": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumns": [
null,
null,
{ "sType": "num-mydate" },
null,
null
]
});

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"num-mydate": function ( a ) {
var x = a.replace( "h ", "." )a.replace( "m", "" );
return parseFloat( x );
}
} );

} );[/code]


Big thanks in advance!

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    With type detection: http://datatables.net/release-datatables/examples/plug-ins/sorting_plugin.html
    Without type detection: http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html

    Allan
  • rasmuslindstromrasmuslindstrom Posts: 7Questions: 0Answers: 0
    Thanks for a really quick answer! Appreciate it =)

    I chose togo with the "without type detection" I think it managed to replace "h " with "." but not the "m" with nothing ""

    if I have theese values
    8h 42m
    9h 24m
    82m 2m
    6h 14m

    When sorting as HTML (asc) it printed out (because it only orders on the first digit (i think))
    6h 14m
    8h 42m
    82m 2m
    9h 24m

    Now with this function, it orders them like this (so the hours are correct)
    6h 14m
    8h 42m
    9h 24m
    82m 2m


    But if I add "8h 8m" it shows this new efter "8h 42m" like this:
    6h 14m
    8h 42m
    8h 8m
    9h 24m
    82m 2m


    What have I been missing? Here's my code:
    [code]jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( "h ", "." ).replace( "m", "" );
    var y = (b == "-") ? 0 : b.replace( "h ", "." ).replace( "m", "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
    var x = (a == "-") ? 0 : a.replace( "h ", "." ).replace( "m", "" );
    var y = (b == "-") ? 0 : b.replace( "h ", "." ).replace( "m", "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };


    $('#example').dataTable( {
    "bPaginate": true,
    "bLengthChange": true,
    "bFilter": true,
    "bSort": true,
    "bInfo": true,
    "bAutoWidth": true,
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "aoColumns": [
    null,
    null,
    { "sType": "numeric-comma" },
    null,
    null
    ]
    });[/code]
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    I'd suggest echoing out to the console what the values are that are computed by the sorting plug-in. It might be an idea to change the name from 'numeric-comma' to something more appropriate as well :-)

    Allan
This discussion has been closed.