Fix: any-number extension should work with non-sting values (ie. numbers!)

gh#455
pull/412/merge
Allan Jardine 1 year ago
parent 244f788ecf
commit 257a4c7c22

@ -1,4 +1,51 @@
/*! © SpryMedia Ltd, David Konrad - datatables.net/license */ /*! © SpryMedia Ltd, David Konrad - datatables.net/license */
(function( factory ){
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery', 'datatables.net'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
var jq = require('jquery');
var cjsRequires = function (root, $) {
if ( ! $.fn.dataTable ) {
require('datatables.net')(root, $);
}
};
if (typeof window === 'undefined') {
module.exports = function (root, $) {
if ( ! root ) {
// CommonJS environments without a window global must pass a
// root. This will give an error otherwise
root = window;
}
if ( ! $ ) {
$ = jq( root );
}
cjsRequires( root, $ );
return factory( $, root, root.document );
};
}
else {
cjsRequires( window, jq );
module.exports = factory( jq, window, window.document );
}
}
else {
// Browser
factory( jQuery, window, document );
}
}(function( $, window, document ) {
'use strict';
var DataTable = $.fn.dataTable;
/** /**
* Sorts columns by any number, ignoring text. This plugin is useful if you have * Sorts columns by any number, ignoring text. This plugin is useful if you have
* mixed content in a column, but still want to sort by numbers. Any number means * mixed content in a column, but still want to sort by numbers. Any number means
@ -29,10 +76,14 @@
*/ */
function _anyNumberSort(a, b, high) { function _anyNumberSort(a, b, high) {
var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/; var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;
if (typeof a === 'string') {
a = a.replace(',', '.').match(reg); a = a.replace(',', '.').match(reg);
a = a !== null ? parseFloat(a[0]) : high; a = a !== null ? parseFloat(a[0]) : high;
}
if (typeof b === 'string') {
b = b.replace(',', '.').match(reg); b = b.replace(',', '.').match(reg);
b = b !== null ? parseFloat(b[0]) : high; b = b !== null ? parseFloat(b[0]) : high;
}
return a < b ? -1 : a > b ? 1 : 0; return a < b ? -1 : a > b ? 1 : 0;
} }
DataTable.ext.type.order['any-number-asc'] = function (a, b) { DataTable.ext.type.order['any-number-asc'] = function (a, b) {
@ -41,3 +92,7 @@ DataTable.ext.type.order['any-number-asc'] = function (a, b) {
DataTable.ext.type.order['any-number-desc'] = function (a, b) { DataTable.ext.type.order['any-number-desc'] = function (a, b) {
return _anyNumberSort(a, b, Number.NEGATIVE_INFINITY) * -1; return _anyNumberSort(a, b, Number.NEGATIVE_INFINITY) * -1;
}; };
return DataTable;
}));

@ -1,2 +1,2 @@
/*! © SpryMedia Ltd, David Konrad - datatables.net/license */ /*! © SpryMedia Ltd, David Konrad - datatables.net/license */
!function(t){var r,u;"function"==typeof define&&define.amd?define(["jquery","datatables.net"],function(e){return t(e,window,document)}):"object"==typeof exports?(r=require("jquery"),u=function(e,n){n.fn.dataTable||require("datatables.net")(e,n)},"undefined"==typeof window?module.exports=function(e,n){return e=e||window,n=n||r(e),u(e,n),t(n,0,e.document)}:(u(window,r),module.exports=t(r,window,window.document))):t(jQuery,window,document)}(function(e,n,t,r){"use strict";e=e.fn.dataTable;function u(e,n,t){var r=/[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;return(e=null!==(e=e.replace(",",".").match(r))?parseFloat(e[0]):t)<(n=null!==(n=n.replace(",",".").match(r))?parseFloat(n[0]):t)?-1:n<e?1:0}return e.ext.type.order["any-number-asc"]=function(e,n){return u(e,n,Number.POSITIVE_INFINITY)},e.ext.type.order["any-number-desc"]=function(e,n){return-1*u(e,n,Number.NEGATIVE_INFINITY)},e}); !function(t){var r,o;"function"==typeof define&&define.amd?define(["jquery","datatables.net"],function(e){return t(e,window,document)}):"object"==typeof exports?(r=require("jquery"),o=function(e,n){n.fn.dataTable||require("datatables.net")(e,n)},"undefined"==typeof window?module.exports=function(e,n){return e=e||window,n=n||r(e),o(e,n),t(n,0,e.document)}:(o(window,r),module.exports=t(r,window,window.document))):t(jQuery,window,document)}(function(e,n,t){"use strict";e=e.fn.dataTable;function r(e,n,t){var r=/[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;return(e="string"==typeof e?null!==(e=e.replace(",",".").match(r))?parseFloat(e[0]):t:e)<(n="string"==typeof n?null!==(n=n.replace(",",".").match(r))?parseFloat(n[0]):t:n)?-1:n<e?1:0}return e.ext.type.order["any-number-asc"]=function(e,n){return r(e,n,Number.POSITIVE_INFINITY)},e.ext.type.order["any-number-desc"]=function(e,n){return-1*r(e,n,Number.NEGATIVE_INFINITY)},e});

@ -1,2 +1,2 @@
/*! © SpryMedia Ltd, David Konrad - datatables.net/license */ /*! © SpryMedia Ltd, David Konrad - datatables.net/license */
import jQuery from"jquery";import DataTable from"datatables.net";let $=jQuery;function _anyNumberSort(e,r,a){var t=/[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;return(e=null!==(e=e.replace(",",".").match(t))?parseFloat(e[0]):a)<(r=null!==(r=r.replace(",",".").match(t))?parseFloat(r[0]):a)?-1:r<e?1:0}DataTable.ext.type.order["any-number-asc"]=function(e,r){return _anyNumberSort(e,r,Number.POSITIVE_INFINITY)},DataTable.ext.type.order["any-number-desc"]=function(e,r){return-1*_anyNumberSort(e,r,Number.NEGATIVE_INFINITY)};export default DataTable; import jQuery from"jquery";import DataTable from"datatables.net";let $=jQuery;function _anyNumberSort(e,r,t){var a=/[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;return(e="string"==typeof e?null!==(e=e.replace(",",".").match(a))?parseFloat(e[0]):t:e)<(r="string"==typeof r?null!==(r=r.replace(",",".").match(a))?parseFloat(r[0]):t:r)?-1:r<e?1:0}DataTable.ext.type.order["any-number-asc"]=function(e,r){return _anyNumberSort(e,r,Number.POSITIVE_INFINITY)},DataTable.ext.type.order["any-number-desc"]=function(e,r){return-1*_anyNumberSort(e,r,Number.NEGATIVE_INFINITY)};export default DataTable;

@ -36,10 +36,14 @@ let $ = jQuery;
*/ */
function _anyNumberSort(a, b, high) { function _anyNumberSort(a, b, high) {
var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/; var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;
if (typeof a === 'string') {
a = a.replace(',', '.').match(reg); a = a.replace(',', '.').match(reg);
a = a !== null ? parseFloat(a[0]) : high; a = a !== null ? parseFloat(a[0]) : high;
}
if (typeof b === 'string') {
b = b.replace(',', '.').match(reg); b = b.replace(',', '.').match(reg);
b = b !== null ? parseFloat(b[0]) : high; b = b !== null ? parseFloat(b[0]) : high;
}
return a < b ? -1 : a > b ? 1 : 0; return a < b ? -1 : a > b ? 1 : 0;
} }
DataTable.ext.type.order['any-number-asc'] = function (a, b) { DataTable.ext.type.order['any-number-asc'] = function (a, b) {

@ -33,10 +33,17 @@ import DataTable from 'datatables.net';
function _anyNumberSort(a, b, high) { function _anyNumberSort(a, b, high) {
var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/; var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;
if (typeof a === 'string') {
a = a.replace(',', '.').match(reg); a = a.replace(',', '.').match(reg);
a = a !== null ? parseFloat(a[0]) : high; a = a !== null ? parseFloat(a[0]) : high;
}
if (typeof b === 'string') {
b = b.replace(',', '.').match(reg); b = b.replace(',', '.').match(reg);
b = b !== null ? parseFloat(b[0]) : high; b = b !== null ? parseFloat(b[0]) : high;
}
return a < b ? -1 : a > b ? 1 : 0; return a < b ? -1 : a > b ? 1 : 0;
} }

Loading…
Cancel
Save