Jump to content


Sorting a columm date

sorting datatable date

  • Please log in to reply
9 replies to this topic

#1 Rafael Alves

Rafael Alves

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationCuritiba, Brazil

Posted 09 October 2012 - 04:54 PM

I have a little problem.
Sorting a columm date in the format d/m/Y, the ordenation is made only for days.

example:

date
01/10/2012
03/09/2012
04/10/2012
08/11/2011

Please help me to solve this problem.

#2 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 09 October 2012 - 07:55 PM

what a theme do you use? datatables?

#3 Rafael Alves

Rafael Alves

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationCuritiba, Brazil

Posted 15 October 2012 - 07:27 PM

yes datatables

#4 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 16 October 2012 - 06:00 AM

you can split the date into three columns:

day | month | year
10 12 2012

#5 Rafael Alves

Rafael Alves

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationCuritiba, Brazil

Posted 18 October 2012 - 06:41 PM

Hi Victor,

i resolved with callback_collumn


function _date($value, $row)
{
return "<span style='visibility:hidden;display:none;'>".date('Y-m-d H:i:s', strtotime($value))."</span>".date('d/m/Y H:i:s', strtotime($value)).";
}

wasn't more correct, but worked

Thanks for help

#6 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 18 October 2012 - 06:46 PM

It's a good solution))



#7 Patos

Patos

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 17 September 2013 - 09:03 AM

Hi, it's coming late.

but try to add in datatables.js :

 

 

$.fn.dataTableExt.aTypes.unshift(
function ( sData ) {
var reg=new RegExp("(0[0-9]|[12][0-9]|3[01])/(0[0-9]|1[012])/(00|19|20|21)[0-9]{2}","g");
if (sData.match(reg))
{ return 'uk_date';
} else { }
return null;
}
);
$.fn.dataTableExt.oSort['uk_date-asc']  = function(a,b) {
    var ukDatea = a.split('/');
    var ukDateb = b.split('/');
     
    var x = parseInt(ukDatea[2] + ukDatea[1] + ukDatea[0]);
    var y = parseInt(ukDateb[2] + ukDateb[1] + ukDateb[0]);
     
    return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};


$.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
    var ukDatea = a.split('/');
    var ukDateb = b.split('/');
     
    var x = parseInt(ukDatea[2] + ukDatea[1] + ukDatea[0]);
    var y = parseInt(ukDateb[2] + ukDateb[1] + ukDateb[0]);
     
    return ((x < y) ? 1 : ((x > y) ?  -1 : 0));
};

and this callback function in your controller on the date field

 

public function _callback_date($value, $row) {
        $date = explode("-",$value);
        return $date[2]."/".$date[1]."/".$date[0];
    }


#8 pdalfarr

pdalfarr

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 12 November 2013 - 07:15 PM

+1 for Rafael Alves's solution: should be part of grocery CRUD (as an optional feature)

 

(Didn't tested Patos's solution since I don't want to modify Grocery CRUD files, but could be good as well)



#9 archerwisdom

archerwisdom

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 21 November 2013 - 01:35 PM

 Rafael Alves's solution would have an issue on export function. The span code will appear in the exported xls file



#10 MiamMiam

MiamMiam

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationLimousin - France

Posted 16 August 2014 - 02:52 PM

@Rafael Alves : it works great, thanks !


Grocery CRUD is great ! | Contribute or donate... or do both ;-)






Also tagged with one or more of these keywords: sorting datatable, date

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users