⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Sorting a columm date



Rafael Alves
  • profile picture
  • Member

Posted 09 October 2012 - 16: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

[color=#282828][font=helvetica, arial, sans-serif]Please help me to solve this problem.[/font][/color]

victor
  • profile picture
  • Member

Posted 09 October 2012 - 19:55 PM

what a theme do you use? datatables?

Rafael Alves
  • profile picture
  • Member

Posted 15 October 2012 - 19:27 PM

yes [color=#282828][font=helvetica, arial, sans-serif]datatables[/font][/color]

victor
  • profile picture
  • Member

Posted 16 October 2012 - 06:00 AM

you can split the date into three columns:

day | month | year
10 12 2012

Rafael Alves
  • profile picture
  • Member

Posted 18 October 2012 - 18: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

victor
  • profile picture
  • Member

Posted 18 October 2012 - 18:46 PM

It's a good solution))


Patos
  • profile picture
  • Member

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

pdalfarr
  • profile picture
  • Member

Posted 12 November 2013 - 19: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)


archerwisdom
  • profile picture
  • Member

Posted 21 November 2013 - 13:35 PM

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


MiamMiam
  • profile picture
  • Member

Posted 16 August 2014 - 14:52 PM

@Rafael Alves : it works great, thanks !