Created
November 26, 2011 09:31
-
-
Save kiecoorp/1395375 to your computer and use it in GitHub Desktop.
Add DateTime Sort to Datatables Jquery Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Add DateTime Sort to Datatables Jquery Plugin | |
// Format 23-10-2011 12:34 | |
// | |
jQuery.fn.dataTableExt.aTypes.push( | |
function ( sData ) { | |
if ( sData.charAt(2) == '-' && sData.charAt(5) == '-' && sData.length == 16) { // Choose format 23-10-2011 18:30 ou 23/10/2011 13:20 | |
return 'date'; | |
} | |
} | |
); | |
function trim(str) { | |
str = str.replace(/^\s+/, ''); | |
for (var i = str.length - 1; i >= 0; i--) { | |
if (/\S/.test(str.charAt(i))) { | |
str = str.substring(0, i + 1); | |
break; | |
} | |
} | |
return str; | |
} | |
function dateHeight(dateStr){ | |
if (trim(dateStr) != '') { | |
var frDate = trim(dateStr).split(' '); | |
var frTime = frDate[1].split(':'); | |
var frDateParts = frDate[0].split('-'); // Choose format 23-10-2011 ou 23/10/2011 | |
var day = frDateParts[0] * 60 * 24; | |
var month = frDateParts[1] * 60 * 24 * 31; | |
var year = frDateParts[2] * 60 * 24 * 366; | |
var hour = frTime[0] * 60; | |
var minutes = frTime[1]; | |
var x = day+month+year+hour+minutes; | |
} else { | |
var x = 99999999999999999; //GoHorse! | |
} | |
return x; | |
} | |
jQuery.fn.dataTableExt.oSort['date-asc'] = function(a,b) { | |
var x = dateHeight(a); | |
var y = dateHeight(b); | |
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); | |
}; | |
jQuery.fn.dataTableExt.oSort['date-desc'] = function(a,b) { | |
var x = dateHeight(a); | |
var y = dateHeight(b); | |
return ((x < y) ? 1 : ((x > y) ? -1 : 0)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you know JavaScript has Infinity?