Merge pull request #73 from jallakim/naturalsort

Fix #72 by implementing the Natural Sort plugin for DataTables.
This commit is contained in:
Khanh Ngo 2016-07-06 09:24:22 +07:00 committed by GitHub
commit b94ae55960
4 changed files with 121 additions and 1 deletions

View File

@ -0,0 +1,114 @@
/**
* Data can often be a complicated mix of numbers and letters (file names
* are a common example) and sorting them in a natural manner is quite a
* difficult problem.
*
* Fortunately a deal of work has already been done in this area by other
* authors - the following plug-in uses the [naturalSort() function by Jim
* Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) to provide natural sorting in DataTables.
*
* @name Natural sorting
* @summary Sort data with a mix of numbers and letters _naturally_.
* @author [Jim Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support)
* @author [Michael Buehler] (https://github.com/AnimusMachina)
*
* @example
* $('#example').dataTable( {
* columnDefs: [
* { 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() {
/*
* Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
* Author: Jim Palmer (based on chunking idea from Dave Koelle)
* Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
* See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
*/
function naturalSort (a, b, html) {
var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
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})/,
hre = /^0x[0-9a-f]+$/i,
ore = /^0/,
htmre = /(<([^>]+)>)/ig,
// convert all to strings and trim()
x = a.toString().replace(sre, '') || '',
y = b.toString().replace(sre, '') || '';
// remove html from strings if desired
if (!html) {
x = x.replace(htmre, '');
y = y.replace(htmre, '');
}
// chunk/tokenize
var xN = x.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
xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)),
yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null;
// first try and sort Hex codes or Dates
if (yD) {
if ( xD < yD ) {
return -1;
}
else if ( xD > yD ) {
return 1;
}
}
// natural sorting through split numeric strings and default strings
for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
// find floats not starting with '0', string or 0 if not defined (Clint Priest)
var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc], 10) || xN[cLoc] || 0;
var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc], 10) || yN[cLoc] || 0;
// handle numeric vs string comparison - number < string - (Kyle Adams)
if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {
return (isNaN(oFxNcL)) ? 1 : -1;
}
// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
else if (typeof oFxNcL !== typeof oFyNcL) {
oFxNcL += '';
oFyNcL += '';
}
if (oFxNcL < oFyNcL) {
return -1;
}
if (oFxNcL > oFyNcL) {
return 1;
}
}
return 0;
}
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"natural-asc": function ( a, b ) {
return naturalSort(a,b,true);
},
"natural-desc": function ( a, b ) {
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;
}
} );
}());

View File

@ -0,0 +1 @@
!function(){function e(e,a,r){var t=/(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,n=/(^[ ]*|[ ]*$)/g,c=/(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,l=/^0x[0-9a-f]+$/i,u=/^0/,i=/(<([^>]+)>)/gi,p=e.toString().replace(n,"")||"",f=a.toString().replace(n,"")||"";r||(p=p.replace(i,""),f=f.replace(i,""));var o=p.replace(t,"\x00$1\x00").replace(/\0$/,"").replace(/^\0/,"").split("\x00"),s=f.replace(t,"\x00$1\x00").replace(/\0$/,"").replace(/^\0/,"").split("\x00"),d=parseInt(p.match(l),10)||1!==o.length&&p.match(c)&&Date.parse(p),h=parseInt(f.match(l),10)||d&&f.match(c)&&Date.parse(f)||null;if(h){if(h>d)return-1;if(d>h)return 1}for(var x=0,m=Math.max(o.length,s.length);m>x;x++){var g=!(o[x]||"").match(u)&&parseFloat(o[x],10)||o[x]||0,$=!(s[x]||"").match(u)&&parseFloat(s[x],10)||s[x]||0;if(isNaN(g)!==isNaN($))return isNaN(g)?1:-1;if(typeof g!=typeof $&&(g+="",$+=""),$>g)return-1;if(g>$)return 1}return 0}jQuery.extend(jQuery.fn.dataTableExt.oSort,{"natural-asc":function(a,r){return e(a,r,!0)},"natural-desc":function(a,r){return-1*e(a,r,!0)},"natural-nohtml-asc":function(a,r){return e(a,r,!1)},"natural-nohtml-desc":function(a,r){return-1*e(a,r,!1)}})}();

View File

@ -178,6 +178,8 @@
<!-- DataTables -->
<script src="{{ url_for('static', filename='adminlte2/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ url_for('static', filename='adminlte2/plugins/datatables/dataTables.bootstrap.min.js') }}"></script>
<!-- DataTables Natural Sort -->
<script src="{{ url_for('static', filename='adminlte2/plugins/datatables/extensions/NaturalSort/natural.min.js') }}"></script>
<!-- Sparkline -->
<script src="{{ url_for('static', filename='adminlte2/plugins/sparkline/jquery.sparkline.min.js') }}"></script>
<!-- Slimscroll -->

View File

@ -143,7 +143,10 @@
"language": {
"lengthMenu": " _MENU_ records"
},
"retrieve" : true
"retrieve" : true,
"columnDefs": [
{ type: 'natural', targets: [0, 4] }
]
});
// handle delete button