DataTables
Advanced interaction
features for your tables.
Editor
Comprehensive editing
library for DataTables.
Manual
Download
Examples
Manual
Reference
Extensions
Plug-ins
Blog
Forums
Discussions
Sign In
Support
FAQs
Download
Purchase
≡
Show site navigation
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: 63,523
Questions: 1
Answers: 10,473
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.
Sign In
·
Register
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Register
Quick Links
Categories
Recent Discussions
Unanswered
Categories
74.3K
All Categories
56
Priority support
24K
Free community support
997
General
14
Announcements
2.5K
DataTables
79
DataTables 2
1.3K
DataTables 1.10
92
DataTables 1.9
35
DataTables 1.8
9
CloudTables
2.2K
Editor
2.8K
Extensions
20
AutoFill
310
Buttons
50
ColVis
30
DateTime
68
FixedColumns
50
FixedHeader
32
ColReorder
30
KeyTable
103
Responsive
23
RowReorder
42
Scroller
163
SearchBuilder
193
SearchPanes
107
Select
26
StateRestore
22
TableTools
220
Bug reports
67
Feature requests
100
Plug-ins
11
Blog
69
Web-site
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 }
]
}
});