EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Darrel on October 30, 2018, 01:56:03 PM



Title: Datagrid Datetime Column Sort [Solved]
Post by: Darrel on October 30, 2018, 01:56:03 PM
Hello Team,

Basically I wanted to try out sorting on the datagrid column on a date field. The following url that I referred for doing this custom sorting is https://www.jeasyui.com/tutorial/datagrid/datagrid14.php (https://www.jeasyui.com/tutorial/datagrid/datagrid14.php)

I had to tweak it a little for dd/mm/yyyy format. But when I introduced the timestamp into the date field, that's when the results started getting weird. The same can be checked in the following fiddle: http://jsfiddle.net/c9so6p3b/ (http://jsfiddle.net/c9so6p3b/)

You can change the sorting for the date field and check the results. The output isn't as expected, notice the timestamp part. Please could you help me out. One thing I have in mind is to separate out the time stamp before doing the comparison but then I got stuck on when I need to add it back :(

Also just another thought, how would this work in case the date string is in dd/Mon/yyyy format....

Regards,
Darrel


Title: Re: Datagrid Datetime Column Sort
Post by: stworthy on October 30, 2018, 05:58:42 PM
Try to define the 'sorter' function as:
Code:
sorter:function(date1,date2){
  var toDate = function(str){
    var parts = str.split(' ');
    var dd = parts[0].split('/');
    var hh = parts[1].split(':');
    var date = new Date(dd[2],parseInt(dd[1])-1,dd[0],hh[0],hh[1],hh[2]);
    return date;
  }
  var d1 = toDate(date1);
  var d2 = toDate(date2);
  return d1.getTime()<d2.getTime()?-1:1;
}


Title: Re: Datagrid Datetime Column Sort
Post by: Darrel on October 31, 2018, 10:23:17 PM
Hello stworthy,

Thanks a lot for your reply. It works as expected.

The below code is my modified version which handles the date in dd/Mon/yyyy format as well.

Code:
var month_names = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];

function dateSorter(date1, date2)
{
var toDate = function(str){
var parts = str.split(' ');
var dd = parts[0].split('/');
var hh = parts[1].split(':');
var date = "";
try{
date = new Date(dd[2],month_names.indexOf(dd[1].substring(0,3).toLowerCase()),dd[0],hh[0],hh[1],hh[2]); //converting month to lower case
}
catch(Ex){
date = new Date(dd[2],parseInt(dd[1])-1,dd[0],hh[0],hh[1],hh[2]); //subtracting one from month since months start from 0 in javascript
}
return date;
}
var d1 = toDate(date1);
var d2 = toDate(date2);
return d1.getTime()<d2.getTime()?-1:1;
}

Thanks & Regards,
Darrel.