≡
×
Plus
Manual
Examples
Reference
Download
Blog
Community
Support
Sorting by date (DD.MM.YYYY)
Sorting by date (DD.MM.YYYY)
afarber
Posts: 53
Questions: 0
Answers: 0
June 2011
edited June 2011
in
General
Hello,
please point me to the right direction on how to implement sorting by dates (like 31.12.2011)
Here is the page where I currently have problems: http://preferans.de/user.php?id=DE7692
Thank you!
Alex
Replies
allan
Posts: 65,830
Questions: 1
Answers: 10,953
Site admin
June 2011
Hi Alex,
A small modification to this plug-in (dots rather than slashes) will do it: http://datatables.net/plug-ins/sorting#date_euro_short
Allan
afarber
Posts: 53
Questions: 0
Answers: 0
June 2011
Thank you, but could I maybe as alternative add an hidden column with numeric timestamps?
I don't know how to use them for sorting though... (i.e. when clicking the Date column, the hidden timestamps column should be used for sorting)
Regards
Alex
afarber
Posts: 53
Questions: 0
Answers: 0
June 2011
Hello Allan,
I've looked at the plugin you've suggested, but don't understand what would 'uk_date-desc' string mean? How do I use it in my case?
Also after looking at http://www.datatables.net/usage/columns I have added this code:
$("#comments").dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumns": [
/* day */ { "bSearchable": false, "bSortable": true, "sType": "date" },
/* about */ { "bSearchable": true, "bSortable": false },
/* author */ { "bSearchable": false, "bSortable": false }
],
but then the 1st column day isn't sorting anything (I click it, the v changes to ^, but nothing happens). Is it because JavaScript's date() doesn't recognize DD.MM.YYYY? I don't see any errors in the console.
Thank you
Alex
afarber
Posts: 53
Questions: 0
Answers: 0
June 2011
I've figured it out, thanks for the pointers:
$(function() {
jQuery.fn.dataTableExt.oSort['day-asc'] = function(a, b) {
var daya = a.split('.');
var dayb = b.split('.');
var x = (daya[2] + daya[1] + daya[0]) * 1;
var y = (dayb[2] + dayb[1] + dayb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['day-desc'] = function(a, b) {
var daya = a.split('.');
var dayb = b.split('.');
var x = (daya[2] + daya[1] + daya[0]) * 1;
var y = (dayb[2] + dayb[1] + dayb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$("#comments").dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaSorting": [[0, "desc"]],
"aoColumns": [
/* day */ { "bSearchable": false, "bSortable": true, "sType": "day" },
/* about */ { "bSearchable": true, "bSortable": false },
/* author */ { "bSearchable": false, "bSortable": false }
]
}
});
This discussion has been closed.
Replies
A small modification to this plug-in (dots rather than slashes) will do it: http://datatables.net/plug-ins/sorting#date_euro_short
Allan
I don't know how to use them for sorting though... (i.e. when clicking the Date column, the hidden timestamps column should be used for sorting)
Regards
Alex
I've looked at the plugin you've suggested, but don't understand what would 'uk_date-desc' string mean? How do I use it in my case?
Also after looking at http://www.datatables.net/usage/columns I have added this code:
$("#comments").dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumns": [
/* day */ { "bSearchable": false, "bSortable": true, "sType": "date" },
/* about */ { "bSearchable": true, "bSortable": false },
/* author */ { "bSearchable": false, "bSortable": false }
],
but then the 1st column day isn't sorting anything (I click it, the v changes to ^, but nothing happens). Is it because JavaScript's date() doesn't recognize DD.MM.YYYY? I don't see any errors in the console.
Thank you
Alex
$(function() {
jQuery.fn.dataTableExt.oSort['day-asc'] = function(a, b) {
var daya = a.split('.');
var dayb = b.split('.');
var x = (daya[2] + daya[1] + daya[0]) * 1;
var y = (dayb[2] + dayb[1] + dayb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['day-desc'] = function(a, b) {
var daya = a.split('.');
var dayb = b.split('.');
var x = (daya[2] + daya[1] + daya[0]) * 1;
var y = (dayb[2] + dayb[1] + dayb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$("#comments").dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaSorting": [[0, "desc"]],
"aoColumns": [
/* day */ { "bSearchable": false, "bSortable": true, "sType": "day" },
/* about */ { "bSearchable": true, "bSortable": false },
/* author */ { "bSearchable": false, "bSortable": false }
]
}
});