When a DataTable is initialised, each column is scanned automatically for the type of data it contains, which in turn allows DataTables to apply the require type of sorting function. There are three built-in types (string, date and numeric) but this can readily be expanded using the functions below. This can make installed a sorting plug-in much easier since you need not specify the sType for the column - it will be picked up automatically.
|
Commas for decimal place
Show details |
Automatically detect numbers which use a comma in the place of a decimal point to allow them to be sorted numerically. |
| Author: | Allan Jardine |
| Code: |
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
var sValidChars = "0123456789-,";
var Char;
var bDecimal = false;
/* Check the numeric part */
for ( i=0 ; i<sData.length ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
/* Only allowed one decimal place... */
if ( Char == "," )
{
if ( bDecimal )
{
return null;
}
bDecimal = true;
}
}
return 'numeric-comma';
}
);
|
|
Currency
Show details |
This plug-in will add automatic detection for currency columns to DataTables. Note that only $ and £ symbols are detected with this code, but it is trivial to add more or change the current ones. This is best used in conjunction with the currency sorting plug-in. |
| Author: | Allan Jardine |
| Code: |
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
var sValidChars = "0123456789.-,";
var Char;
/* Check the numeric part */
for ( i=1 ; i<sData.length ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
}
/* Check prefixed by currency */
if ( sData.charAt(0) == '$' || sData.charAt(0) == '£' )
{
return 'currency';
}
return null;
}
);
|
|
Numbers with HTML
Show details |
This type-detection plug-in will look at an HTML string from a data cell, strip the HTML tags and then check to see if the remaining data is numeric. If it is, then the data can be sorted numerically with the Numbers with HTML sorting plug-in. |
| Author: | Allan Jardine |
| Code: |
jQuery.fn.dataTableExt.aTypes.push( function ( sData )
{
sData = typeof sData.replace == 'function' ?
sData.replace( /<.*?>/g, "" ) : sData;
var sValidFirstChars = "0123456789-";
var sValidChars = "0123456789.";
var Char;
var bDecimal = false;
/* Check for a valid first char (no period and allow negatives) */
Char = sData.charAt(0);
if (sValidFirstChars.indexOf(Char) == -1)
{
return null;
}
/* Check all the other characters are valid */
for ( var i=1 ; i<sData.length ; i++ )
{
Char = sData.charAt(i);
if (sValidChars.indexOf(Char) == -1)
{
return null;
}
/* Only allowed one decimal place... */
if ( Char == "." )
{
if ( bDecimal )
{
return null;
}
bDecimal = true;
}
}
return 'num-html';
} );
|
|
Week day detection
Show details |
Automatically detect week days (English) and sort them chronologically rather than alphabetically. Goes with the week day sorting function pair. |
| Author: | Pål Oliver Kristiansen |
| Code: |
jQuery.fn.dataTableExt.aTypes.push(
function ( sData ){
var sValidStrings = 'monday,tuesday,wednesday,thursday,friday,saturday,sunday';
if (sValidStrings.indexOf(sData.toLowerCase()) >= 0){
return 'weekdays-sort';
}
return null;
}
);
|
Note that all contributed code is copyright to the original author, unless otherwise stated.