Use regex for html stripping. Let users choose if they want to strip html or not.

pull/235/head
Michael Buehler 9 years ago
parent ad1126dcdc
commit a73f4bb70b

@ -17,6 +17,15 @@
* { type: 'natural', targets: 0 } * { type: 'natural', targets: 0 }
* ] * ]
* } ); * } );
*
* Html can be stripped from sorting by using 'natural-nohtml' such as
*
* $('#example').dataTable( {
* columnDefs: [
* { type: 'natural-nohtml', targets: 0 }
* ]
* } );
*
*/ */
(function() { (function() {
@ -27,24 +36,27 @@
* Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
* See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
*/ */
function naturalSort (a, b) { function naturalSort (a, b, html) {
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
sre = /(^[ ]*|[ ]*$)/g, sre = /(^[ ]*|[ ]*$)/g,
dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
hre = /^0x[0-9a-f]+$/i, hre = /^0x[0-9a-f]+$/i,
ore = /^0/, ore = /^0/,
htmre = /(<([^>]+)>)/ig,
// convert all to strings and trim() // convert all to strings and trim()
x = a.toString().replace(sre, '') || '', x = a.toString().replace(sre, '') || '',
y = b.toString().replace(sre, '') || '', y = b.toString().replace(sre, '') || '';
//strip html from the strings // remove html from strings if desired
xH = $(x).text(), if (!html) {
yH = $(y).text(), x = x.replace(htmre, '');
y = y.replace(htmre, '');
}
// chunk/tokenize // chunk/tokenize
xN = xH.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), var xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
yN = yH.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
// numeric, hex or date detection // numeric, hex or date detection
xD = parseInt(xH.match(hre), 10) || (xN.length !== 1 && xH.match(dre) && Date.parse(xH)), xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)),
yD = parseInt(yH.match(hre), 10) || xD && yH.match(dre) && Date.parse(yH) || null; yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null;
// first try and sort Hex codes or Dates // first try and sort Hex codes or Dates
if (yD) { if (yD) {
@ -82,11 +94,19 @@ function naturalSort (a, b) {
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"natural-asc": function ( a, b ) { "natural-asc": function ( a, b ) {
return naturalSort(a,b); return naturalSort(a,b,true);
}, },
"natural-desc": function ( a, b ) { "natural-desc": function ( a, b ) {
return naturalSort(a,b) * -1; return naturalSort(a,b,true) * -1;
},
"natural-nohtml-asc": function( a, b ) {
return naturalSort(a,b,false);
},
"natural-nohtml-desc": function( a, b ) {
return naturalSort(a,b,false) * -1;
} }
} ); } );

Loading…
Cancel
Save