Bamboozled over Date sorting

Bamboozled over Date sorting

basilbearbasilbear Posts: 10Questions: 0Answers: 0
edited October 2009 in Plug-ins
Hi,

I have done my best to follow leads and instructions most of the day, but hey, I'm beat now - sorry!

I ONLY want dd/mm/yy dates to sort properly, so I have done this:

Placed this code in customer.js ahead of the ready function:

[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$/))
{
confirm('matched it!');
return 'uk_date';
}
return null;
}
);

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));
};
[/code]

Now, I notice that the sData.match appears to want '-' between the digits, so I tried changing it without success.

And you can see that I have popped in a confirm() just to let me know we are getting there. Funnily enough that only mentions 3 cols' sData from the first row - excluding dates and some other numbers.

And here is a sample of the table:

[code]



Job No
Sub
Logged On
Priority
Status
Est Completion
Description
Comment




179988
109
22/10/2008
3
Complete
22/10/2008
INSTALALTION OF PRO18E
BOILER C/W ALL THE USUAL


179988
110
30/10/2008
3
Complete
05/01/2009
Gas Install CP1 Test Job




[/code]

So, I will make a cheerful donation to this excellent work IF ONLY I can get it to work.

Please let me know where I have gone wrong.

Thank you.

BasilBear.

Replies

  • allanallan Posts: 55,884Questions: 1Answers: 8,836 Site admin
    Hi BasilBear,

    Is this conversation 'sunk' now because you got it sorted?

    Regards,
    Allan
  • basilbearbasilbear Posts: 10Questions: 0Answers: 0
    Hi Allan,

    yes indeed - I hope that I did the right thing by sinking it.

    I resolved the matter by placing the code provided for the plugins directly into my copy of jquery.datatables.js, having got my head around what was going on.

    I placed "uk_date-asc" and "uk_date-desc" into

    [code] _oExt.oSort = { [/code]

    and I placed

    [code]

    /*
    * UK date match
    *
    */
    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;
    },

    [/code]

    into _oExt.aTypes = [

    NB. I changed the date separators from '-' to '/'.

    I would prefer not to have touched your main js file, but I didn't appear to be getting a proper result by placing the plugin code into my custom.js

    Regards

    Victor.
  • basilbearbasilbear Posts: 10Questions: 0Answers: 0
    Just made you the promised donation.
  • allanallan Posts: 55,884Questions: 1Answers: 8,836 Site admin
    Hi Victor,

    Thanks very much for the donation - much appreciated.

    It certainly should be possible to add the plug-ins without modifying the core, as shown in this demo: http://datatables.net/examples/api/sorting_plugin.html

    The code sequence needs to go:

    1. include DataTables JS file
    2. add the plug-ins
    3. initialise your table

    Regards,
    Allan
This discussion has been closed.