Fix - sorting: Datetime moment was giving an error when used on non-string types

https://datatables.net/forums/discussion/comment/185605#Comment_185605
pull/508/head
Allan Jardine 4 years ago
parent 6ac307f367
commit addb0eb013

@ -28,20 +28,24 @@
} }
}(function ($, moment) { }(function ($, moment) {
function strip (d) {
if ( typeof d === 'string' ) {
// Strip HTML tags and newline characters if possible
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
// Strip out surrounding white space
d = d.trim();
}
return d;
}
$.fn.dataTable.moment = function ( format, locale, reverseEmpties ) { $.fn.dataTable.moment = function ( format, locale, reverseEmpties ) {
var types = $.fn.dataTable.ext.type; var types = $.fn.dataTable.ext.type;
// Add type detection // Add type detection
types.detect.unshift( function ( d ) { types.detect.unshift( function ( d ) {
if ( d ) { d = strip(d);
// Strip HTML tags and newline characters if possible
if ( d.replace ) {
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
}
// Strip out surrounding white space
d = d.trim();
}
// Null and empty values are acceptable // Null and empty values are acceptable
if ( d === '' || d === null ) { if ( d === '' || d === null ) {
@ -55,15 +59,7 @@ $.fn.dataTable.moment = function ( format, locale, reverseEmpties ) {
// Add sorting method - use an integer for the sorting // Add sorting method - use an integer for the sorting
types.order[ 'moment-'+format+'-pre' ] = function ( d ) { types.order[ 'moment-'+format+'-pre' ] = function ( d ) {
if ( d ) { d = strip(d);
// Strip HTML tags and newline characters if possible
if ( d.replace ) {
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
}
// Strip out surrounding white space
d = d.trim();
}
return !moment(d, format, locale, true).isValid() ? return !moment(d, format, locale, true).isValid() ?
(reverseEmpties ? -Infinity : Infinity) : (reverseEmpties ? -Infinity : Infinity) :

Loading…
Cancel
Save