Type detection plug-in development
Ordering plug-ins can be very useful for ordering arbitrary data types, however to be truly useful they need a companion type detection plug-in that will automatically scan a column's data and determine if the ordering plug-in can order the data in the column. Ordering plug-ins can be used without a type detection plug-in through the columns.type
option, but their utility is greatly increased if this just happens for you automatically!
Type detection plug-ins are attached to the DataTable.ext.type.detect
array, and perform a simple operation: does the data given match the requirements for the type or not. If it does the name of the type should be returned (i.e. a string), otherwise null
is returned.
DataTable.ext.type.detect
is an array and its ordering is important. DataTables will start at index 0 and iterate through the array until it finds a match for the data. So to have your plug-in executed before the DataTables default type detection methods, use unshift()
to add your function to the start of the array.
Consider the following:
DataTable.ext.type.detect.unshift( function ( data ) {
if ( typeof data !== 'string' ) {
return null;
}
var units = data.replace( /[\d\.]/g, '' ).toLowerCase();
if ( units !== '' && units !== 'b' && units !== 'kb' && units !== 'mb' && units !== 'gb' ) {
return null;
}
return isNaN( parseFloat( data ) ) ?
null :
'file-size';
} );
This is essentially a validation function - is the data of a given type. In this case we are checking to see if the column contains file size data for the file size ordering plug-in. The first check is to ensure that it is a string we are checking, the second to ensure that it has units and the last to check that the data contains a valid number.
When all three checks pass file-size
is returned so DataTables knows that this data point is valid for the file-size
data type.
Existing plug-ins
You might find that another DataTables user has already created a plug-in that matched your requirements. Plug-ins are published on this site and also available on the DataTables CDN.
Publish your plug-in
If you create an type-detection plug-in for DataTables, please let us know!. Others will likely benefit for your plug-in and I (and the community as a whole) will be very grateful for your contribution.