Help Needing Sorting UK Date Column: dd-mm-yyyy

Help Needing Sorting UK Date Column: dd-mm-yyyy

sm9sm9 Posts: 27Questions: 0Answers: 0
edited December 2009 in General
Hi there,

I wonder if someone can help me? I've got a column in a table in UK date format, but instead of forward slashes, it'd using hyphens, i.e. 08-12-2009. I hope it'd work anyway, but I'm not even sure if I'm copying and pasting the code in the right place? Clicking on the Date column in the page generated below (it;s the 5th of 12 columns) does nothing at all, whilst clicking on the other columns sorts it A to Z.

[code]


jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
if (sData.match(/^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[012])\-(19|20|21)\d\d$/))
{
return 'uk_date';
}
return null;
}
);

$(document).ready( function () {
$('#detail').dataTable( {
"iDisplayLength": 3000,
"sDom": 'T<"clear">lfrtip',
"aoColumns": [
null,
null,
null,
null,
{ "sType": "date" },
null,
null,
null,
null,
null,
null,
null
]
} );
} );



[/code]

Thanks for any help with this!

Stephen

Replies

  • sm9sm9 Posts: 27Questions: 0Answers: 0
    Any ideas on this at all? Below the above code, I have a well formed table. I'm not even sure if I'm putting the uk_date code in the right place, never mind know how to hack it if it didn't sort dates in the dd-mm-yyyy format.

    Thanks,

    Stephen
  • mheathcotemheathcote Posts: 1Questions: 0Answers: 0
    Not validated my theory but here's what I think...

    1) You have the type detection plug in place, but have not added the sort plugin
    2) in aoColumns specify "uk_date"' rather than "date"

    For the sort plug-in, I took the one on the site by Andy McMaster & modifed slightly (change to split by - instead of /, and handle blank dates as highest in sort order. Here is it :
    [code]
    jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {

    var ukDatea = a.split('-');
    var ukDateb = b.split('-');

    //Treat blank/non date formats as highest sort
    if (isNaN(parseInt(ukDatea[0]))) {
    return 1;
    }

    if (isNaN(parseInt(ukDateb[0]))) {
    return -1;
    }

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

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

    jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
    var ukDatea = a.split('-');
    var ukDateb = b.split('-');

    //Treat blank/non date formats as highest sort
    if (isNaN(parseInt(ukDatea[0]))) {
    return -1;
    }

    if (isNaN(parseInt(ukDateb[0]))) {
    return 1;
    }

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

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

    [/code]

    Hope this helps...
  • sm9sm9 Posts: 27Questions: 0Answers: 0
    Hi mheathcote,

    Thanks for this, it's MUCH appreciated! :-) You're right, I just didn't have that code in there - I'm a bit of a thicko with JavaScript and jQuery so thought it might have been built-in already as I couldn't find any specific plugin downloads (I was looking for specific plugin files).

    Here's the exact code I used (with your code) and just had a html table below it.

    Thanks,

    Stephen

    [code]


    jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {

    var ukDatea = a.split('-');
    var ukDateb = b.split('-');

    //Treat blank/non date formats as highest sort
    if (isNaN(parseInt(ukDatea[0]))) {
    return 1;
    }

    if (isNaN(parseInt(ukDateb[0]))) {
    return -1;
    }

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

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

    jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
    var ukDatea = a.split('-');
    var ukDateb = b.split('-');

    //Treat blank/non date formats as highest sort
    if (isNaN(parseInt(ukDatea[0]))) {
    return -1;
    }

    if (isNaN(parseInt(ukDateb[0]))) {
    return 1;
    }

    var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;

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

    jQuery.fn.dataTableExt.aTypes.push(
    function ( sData )
    {
    if (sData.match(/^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[012])\-(19|20|21)\d\d$/))
    {
    return 'uk_date';
    }
    return null;
    }
    );

    $(document).ready( function () {
    $('#detail').dataTable( {
    "iDisplayLength": 3000,
    "sDom": 'T<"clear">lfrtip',
    "aoColumns": [
    null,
    null,
    null,
    null,
    { "sType": "uk_date" },
    null,
    null,
    null,
    null,
    null,
    null,
    null
    ]
    } );
    } );


    [/code]
This discussion has been closed.