File size

Detect abbreviated file size data (8MB, 4KB, 3B, etc)

  • Author: Allan Jardine - datatables.net

Detect file size type columns automatically. Commonly used for computer file sizes, this can allow sorting to take the order of magnitude indicated by the label (GB etc) into account.

Plug-in code

jQuery.fn.dataTable.ext.type.detect.unshift( function ( data ) {
    if ( typeof data !== 'string' ) {
        return null;
    }

    var matches = data.match( /^(\d+(?:\.\d+)?)\s*([a-z]+)/i );
    var units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
    var is_file_size = ( matches && jQuery.inArray(matches[2].toLowerCase(), units) !== -1 );
    return is_file_size ? 'file-size' : null;
} );

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!