1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-03-15 16:29:16 +01:00

New: Currency, formatted numbers and HTML numbers sort types built in

- This commit sees the number of built in type detection and sorting
  functions increase to cover the most common cases of use of plug-ins
  for DataTables (witht he exception of dates, for which a new first
  class plug-in will be created). Specifically, DataTables now has built
  in support for:
  - Dates (Date.parse())
  - Numeric sorting
  - Formatted numbers sorting (including currency and thousands
    seperators)
  - Numbers wrapped in HTML (link tags for example)
  - Formatted numbers in HTML
  - HTML

Although the numeric sorting plug-ins could have been collapsed down to
just two plug-ins (rather than 4) I decided to do it this way to allow
type based filter formatters to be used with the formatted number types
to allow search for "100,000" or "100000" to match the same data. This
is not built in, but it is possible (and might be in future).

- The goal with these additional functions is to enhance the abilities
  of DataTables out of the box to cover the most common cases for data
  usage - DataTables is all about making data in tables more accessable
  after all! The size cost is ~300 bytes for these additional functions
This commit is contained in:
Allan Jardine 2013-08-03 08:08:38 +01:00
parent 59c6b530f3
commit f9ac4c64e2
2 changed files with 139 additions and 65 deletions

View File

@ -1 +1 @@
9a8be62411b197d5b425d2867c0e20035cd5c11c
cbadecf7c8c87fa07a5634861d1c162e5117a0cb

View File

@ -472,8 +472,10 @@
detectedType = types[j]( cache[k] );
// Doesn't match, so break early, since this type can't
// apply to this column
if ( ! detectedType ) {
// apply to this column. Also, HTML is a special case since
// it is so similar to `string`. Just a single match is
// needed for a column to be html type
if ( ! detectedType || detectedType === 'html' ) {
break;
}
}
@ -13516,24 +13518,77 @@
}());
$.extend( DataTable.ext.type.sort, {
/*
* text sorting
*/
"string-pre": function ( a )
{
if ( typeof a != 'string' )
{
if (a === null || a === undefined || !a.toString)
{
return '';
}
a = a.toString();
var _numericReplace = function ( d, re1, re2 ) {
if ( !d || d === '-' ) {
return -Infinity;
}
if ( d.replace ) {
if ( re1 ) {
d = d.replace( re1, '' );
}
return a.toLowerCase();
if ( re2 ) {
d = d.replace( re2, '' );
}
}
return d * 1;
};
$.extend( DataTable.ext.type.sort, {
// Dates
"date-pre": function ( d )
{
return Date.parse( d ) || 0;
},
// string-asc and -desc are retained only for compatibility with
// Plain numbers
"numeric-pre": function ( d )
{
return _numericReplace( d );
},
// Formatted numbers
"numeric-fmt-pre": function ( d )
{
return _numericReplace( d, _re_formatted_numeric );
},
// HTML numeric
"html-numeric-pre": function ( d )
{
return _numericReplace( d, _re_html );
},
// HTML numeric, formatted
"html-numeric-fmt-pre": function ( d )
{
return _numericReplace( d, _re_html, _re_formatted_numeric );
},
// html
"html-pre": function ( a )
{
return a.replace ?
a.replace( /<.*?>/g, "" ).toLowerCase() :
a+'';
},
// string
"string-pre": function ( a )
{
return typeof a === 'string' ?
a.toLowerCase() :
! a || ! a.toString ?
'' :
a.toString();
},
// string-asc and -desc are retained only for compatibility with the old
// sort methods
"string-asc": function ( x, y )
{
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
@ -13542,65 +13597,84 @@
"string-desc": function ( x, y )
{
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
},
/*
* html sorting (ignore html tags)
*/
"html-pre": function ( a )
{
return a.replace( /<.*?>/g, "" ).toLowerCase();
},
/*
* date sorting
*/
"date-pre": function ( a )
{
var x = Date.parse( a );
if ( isNaN(x) || x==="" )
{
x = Date.parse( "01/01/1970 00:00:00" );
}
return x;
},
/*
* numerical sorting
*/
"numeric-pre": function ( a )
{
return (a=="-" || a==="") ? -Infinity : a*1;
}
} );
var _re_formatted_numeric = /[',$£€¥]/g;
var _re_html = /<.*?>/g;
var _empty = function ( d ) {
return !d || d === '-' ? true : false;
};
var _isNumber = function ( d, formatted ) {
if ( formatted && typeof d === 'string' ) {
d = d.replace( _re_formatted_numeric, '' );
}
return !d || d==='-' || (!isNaN( parseFloat(d) ) && isFinite( d ));
};
// A string without HTML in it can be considered to be HTML still
var _isHtml = function ( d ) {
return !d || typeof d === 'string';
};
var _stripHtml = function ( d ) {
return d.replace( _re_html, '' );
};
var _htmlNumeric = function ( d, formatted ) {
var html = _isHtml( d );
return ! html ?
null :
_isNumber( _stripHtml( d ), formatted ) ?
true :
null;
};
// Built in type detection. See model.ext.aTypes for information about
// what is required from this methods.
$.extend( DataTable.ext.type.detect, [
// Numeric data type
function ( data )
{
return data==='' || data==='-' || (!isNaN( parseFloat(data) ) && isFinite( data )) ?
'numeric' : null;
},
// Dates (only those recognised by the browser's Date.parse)
function ( data )
function ( d )
{
var parsed = Date.parse(data);
return (parsed !== null && !isNaN(parsed)) || (typeof data==='string' && data.length===0) ?
'date' : null;
var parsed = Date.parse(d);
return (parsed !== null && !isNaN(parsed)) || _empty(d) ? 'date' : null;
},
// HTML
function ( data )
// Plain numbers
function ( d )
{
return typeof data === 'string' && data.indexOf('<') != -1 && data.indexOf('>') != -1 ?
return _isNumber( d ) ? 'numeric' : null;
},
// Formatted numbers
function ( d )
{
return _isNumber( d, true ) ? 'numeric-fmt' : null;
},
// HTML numeric
function ( d )
{
return _htmlNumeric( d ) ? 'html-numeric' : null;
},
// HTML numeric, formatted
function ( d )
{
return _htmlNumeric( d, true ) ? 'html-numeric-fmt' : null;
},
// HTML (this is strict checking - there much be html)
function ( d )
{
return _empty( d ) || (typeof d === 'string' && d.indexOf('<') !== -1) ?
'html' : null;
}
] );