DataTables logo DataTables

Type detection

It can be useful to have DataTables automatically detect certain data types such that it will use that data type for sorting. DataTables has three built in types ('numeric', 'date' and 'string'), but you may wish to add (for example) a 'currency' type, which will detect (and then sort) numbers with a '£' or '$' sign at the front of them.

This can be done by adding a function to the $.fn.dataTableExt.aTypes types array. This function must accept a single string input and return the name of the type if detected or null if the type is not recognised. The following is an example which will detect a 'currency' type:

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;
	}
);

Note that the built-in types of 'date' and 'numeric' are defined in this array and as such you may replace them if you wish to overrule DataTables' default processing, or use $.fn.dataTableExt.aTypes.unshift( function (s) {...} ); to enter your type detection functions at the start of the array, thus giving them a higher priority.

If a specific type cannot be found for a given column, it will default to 'string'.