diff options
author | Johan Lundberg <lundberg@nordu.net> | 2014-04-02 17:26:12 +0200 |
---|---|---|
committer | Johan Lundberg <lundberg@nordu.net> | 2014-04-02 17:26:12 +0200 |
commit | 8214042cfdd6facad36aae287cbee60d6b96a4ee (patch) | |
tree | 0c1fa1260d0d588a57d953d8b6667df3739d2321 /static/js/dataTables.fileSize.js | |
parent | 807973debd5cb00a4250fe88f7037dcd3cd3133b (diff) |
Added filters and type detection for dataTables.
Diffstat (limited to 'static/js/dataTables.fileSize.js')
-rw-r--r-- | static/js/dataTables.fileSize.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/static/js/dataTables.fileSize.js b/static/js/dataTables.fileSize.js new file mode 100644 index 0000000..86f7812 --- /dev/null +++ b/static/js/dataTables.fileSize.js @@ -0,0 +1,51 @@ +/** + * Created by lundberg on 4/2/14. + */ + +// http://datatables.net/plug-ins/type-detection +// http://datatables.net/plug-ins/sorting + +jQuery.fn.dataTableExt.aTypes.unshift( + function ( sData ) { + var sValidChars = "0123456789"; + var Char; + + /* Check the numeric part */ + for ( i=0 ; i<(sData.length - 3) ; i++ ) + { + Char = sData.charAt(i); + if (sValidChars.indexOf(Char) == -1) + { + return null; + } + } + + /* Check for size unit B, KB, MB or GB */ + if (sData.substring(sData.length - 2, sData.length) == "B" + || sData.substring(sData.length - 2, sData.length) == "KB" + || sData.substring(sData.length - 2, sData.length) == "MB" + || sData.substring(sData.length - 2, sData.length) == "GB" ) + { + return 'file-size'; + } + return null; + } +); +jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "file-size-pre": function ( a ) { + var x = a.substring(0,a.length - 2); + + var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? + 1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1)); + + return parseInt( x * x_unit, 10 ); + }, + + "file-size-asc": function ( a, b ) { + return ((a < b) ? -1 : ((a > b) ? 1 : 0)); + }, + + "file-size-desc": function ( a, b ) { + return ((a < b) ? 1 : ((a > b) ? -1 : 0)); + } +} ); |