sorting german date - please help

sorting german date - please help

pattepatte Posts: 3Questions: 0Answers: 0
edited November 2010 in Plug-ins
Hey guys,

sorry if my question has already been discussed - unfortunately I could not find an answer for my problem.

Can someone please help me out, I am trying to sort a column with a german date and I was not able to get the Date-Plugin working - and.. I have no clue...

01.02.2011 (dd.mm.YY) This is the date format. I used something like this:

[code]

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

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('/');

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));
};

$(document).ready(function() {

$('#events').dataTable( {
"col2": [
null,
null,
null,
{ "sType": "uk_date" },
null
]
} );

});

[/code]

Do I have to modify the date format or the plugin to detect the date in column "col2" ?

Thanks much for any help!

patte

Replies

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi patte,

    The date format that yo have there is using a dot as a separator, where as the plug-in above is using a slash. It should simply be a case of change the 'split':

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

    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('.');

    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));
    };

    $(document).ready(function() {
    $('#events').dataTable( {
    "col2": [
    null,
    null,
    null,
    { "sType": "uk_date" },
    null
    ]
    } );

    });
    [/code]
    Allan
  • pattepatte Posts: 3Questions: 0Answers: 0
    edited November 2010
    Hi Allan,

    thanks much for the quick response!

    Unfortunately that didn`t work - the date column has still been sorted the wrong way... mixing the years ...

    sorting_asc results in :

    01.02.2011
    03.01.2011
    04.01.2011
    07.12.2010
    10.01.2011
    11.01.2011
    13.12.2011
    14.12.2010
    18.11.2010
    20.12.2011

    any ideas?

    Thanks much
    pate
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Hi Pate,

    Sorry one thing I should have picked up on and missed - you've got 'col2' as a parameter which is being passed into DataTables - which DataTables doesn't recognise. If you change that 'col2' to 'aoColumns' - hopefully that will do the trick.

    Allan
  • pattepatte Posts: 3Questions: 0Answers: 0
    Hi Allan,

    ah, you`re right!

    And I also changed the aoCollumns-settings to match die table structure.

    Thanks for the support

    patte
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Nice one - for reference if you don't want to have to specify all columns in aoColumns you can you aoColumnDefs - for example:

    [code]
    $(document).ready(function() {
    $('#events').dataTable( {
    "aoColumnDefs": [
    { "sType": "uk_date", "aTargets": [ 3 ] } ]
    } );

    });
    [/code]
    Regards,
    Allan
This discussion has been closed.