Merge pull request #18 from jhfrench/master

Added sorting support for "dd-MMM-yyyy" formatted dates
pull/19/head
Allan Jardine 12 years ago
commit 1bc8d6cdcd

@ -0,0 +1,47 @@
/*
* Adds a new sorting option to dataTables called <code>date-dd-mmm-yyyy</code>. Also
* includes a type detection plug-in. Matches and sorts date strings in
* the format: <code>(dd/mmm/yyyy</code>. For example:
* <ul>
* <li>02-FEB-1978</li>
* <li>17-MAY-2013</li>
* <li>31-JAN-2014</li>
* </ul>
*
* @name Date dd-mmm-yyyy
* @anchor date-dd-mmm-yyyy
* @author <a href="http://www.appliedinter.net/jeromy_works/">Jeromy French</a>
*/
var customDateDDMMMYYYYToOrd = function (date) {
"use strict"; //let's avoid tom-foolery in this function
// Convert to a number YYYYMMDD which we can use to order
var dateParts = date.split(/-/);
return (dateParts[2] * 10000) + ($.inArray(dateParts[1].toUpperCase(), ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]) * 100) + dateParts[0];
};
// This will help DataTables magic detect the "dd-MMM-yyyy" format; Unshift so that it's the first data type (so it takes priority over existing)
jQuery.fn.dataTableExt.aTypes.unshift(
function (sData) {
"use strict"; //let's avoid tom-foolery in this function
if (/^([0-2]?\d|3[0-1])-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}/i.test(sData)) {
return 'date-dd-mmm-yyyy';
}
return null;
}
);
// define the sorts
jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-asc'] = function (a, b) {
"use strict"; //let's avoid tom-foolery in this function
var ordA = customDateDDMMMYYYYToOrd(a),
ordB = customDateDDMMMYYYYToOrd(b);
return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
};
jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-desc'] = function (a, b) {
"use strict"; //let's avoid tom-foolery in this function
var ordA = customDateDDMMMYYYYToOrd(a),
ordB = customDateDDMMMYYYYToOrd(b);
return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
};
Loading…
Cancel
Save