Rewrite file size sorting and type detection plug-ins to use 1.10 style

and updated code
pull/183/head
Allan Jardine 10 years ago
parent d9cb044f6b
commit 9c2e407535

@ -1,37 +1,36 @@
/** /**
* When dealing with computer file sizes, it is common to append a post fix * When dealing with computer file sizes, it is common to append a post fix
* such as KB, MB or GB to a string in order to easily denote the order of * such as B, KB, MB or GB to a string in order to easily denote the order of
* magnitude of the file size. This plug-in allows sorting to take these * magnitude of the file size. This plug-in allows sorting to take these
* indicates of size into account. A counterpart type detection plug-in * indicates of size into account.
* is also available. *
* A counterpart type detection plug-in is also available.
* *
* @name File size * @name File size
* @summary Sort abbreviated file sizes correctly (8MB, 4KB etc) * @summary Sort abbreviated file sizes correctly (8MB, 4KB, etc)
* @author _anjibman_ * @author Allan Jardine - datatables.net
* *
* @example * @example
* $('#example').dataTable( { * $('#example').DataTable( {
* columnDefs: [ * columnDefs: [
* { type: 'file-size', targets: 0 } * { type: 'file-size', targets: 0 }
* ] * ]
* } ); * } );
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.fn.dataTable.ext.type.order['file-size-pre'] = function ( data ) {
"file-size-pre": function ( a ) { var units = data.replace( /[\d\.]/g, '' ).toLowerCase();
var x = a.substring(0,a.length - 2); var multiplier = 1;
var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? if ( units === 'kb' ) {
1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1)); multiplier = 1000;
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));
} }
} ); else if ( units === 'mb' ) {
multiplier = 1000000;
}
else if ( units === 'gb' ) {
multiplier = 1000000000;
}
return parseFloat( data ) * multiplier;
};

@ -1,36 +1,24 @@
/** /**
* Detect "file size" type columns automatically. Commonly used for computer * Detect file size type columns automatically. Commonly used for computer
* file sizes, this can allow sorting to take the order of magnitude indicated * file sizes, this can allow sorting to take the order of magnitude indicated
* by the label (GB etc) into account. * by the label (GB etc) into account.
* *
* @name File size * @name File size
* @summary Detect abbreviated file size data (8MB, 4KB etc) * @summary Detect abbreviated file size data (8MB, 4KB, 3B, etc)
* @author _anjibman_ * @author Allan Jardine - datatables.net
*/ */
jQuery.fn.dataTableExt.aTypes.unshift( jQuery.fn.dataTable.ext.type.detect.unshift( function ( data ) {
function ( sData ) if ( typeof data !== 'string' ) {
{ return null;
var sValidChars = "0123456789"; }
var Char;
/* Check the numeric part */
for ( var i=0 ; i<(sData.length - 3) ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
}
/* Check for size unit KB, MB or GB */ var units = data.replace( /[\d\.]/g, '' ).toLowerCase();
if ( sData.substring(sData.length - 2, sData.length) == "KB" if ( units !== '' && units !== 'b' && units !== 'kb' && units !== 'mb' && units !== 'gb' ) {
|| sData.substring(sData.length - 2, sData.length) == "MB"
|| sData.substring(sData.length - 2, sData.length) == "GB" )
{
return 'file-size';
}
return null; return null;
} }
);
return isNaN( parseFloat( data ) ) ?
null :
'file-size';
} );
Loading…
Cancel
Save