New - sorting: Dynamic enum

- The old enum was static, while this makes it easy to customse
pull/272/head
Allan Jardine 9 years ago
parent a4479c3976
commit f3d0dfb495

@ -1,37 +1,49 @@
/** /**
* Sort by priority through an enumerated list. In this case the words _High_, * Sort data by a defined enumerated (enum) list. The options for the values in
* _Medium_ and _Low_ are used and thus sorted in priority order. This works * the enum are defined by passing the values in an array to the method
* by converting the works to a numerical value and then sorting based on that * `$.fn.dataTable.enum`. Type detection and sorting plug-ins for DataTables will
* value. * automatically be generated and added to the table.
* *
* @name enum * For full details and instructions please see [this DataTables blog
* @summary Sort an enumerated list of options * post](//datatables.net/blog/2016-06-16).
* @author [Allan Jardine](http://sprymedia.co.uk) *
* @name enum
* @summary Dynamically create enum sorting options for a DataTable
* @author [SpryMedia Ltd](http://datatables.net)
* *
* @example * @example
* $('#example').dataTable( { * $.fn.dataTable.enum( [ 'High', 'Medium', 'Low' ] );
* columnDefs: [ *
* { type: 'enum', targets: 0 } * $('#example').DataTable();
* ]
* } );
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"enum-pre": function ( a ) { (function ($) {
// Add / alter the switch statement below to match your enum list
switch( a ) {
case "High": return 1; var unique = 0;
case "Medium": return 2; var types = $.fn.dataTable.ext.type;
case "Low": return 3;
default: return 4; $.fn.dataTable.enum = function ( arr ) {
} var name = 'enum-'+(unique++);
}, var lookup = window.Map ? new Map() : {};
"enum-asc": function ( a, b ) { for ( var i=0, ien=arr.length ; i<ien ; i++ ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0)); lookup[ arr[i] ] = i;
},
"enum-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
} }
} );
// Add type detection
types.detect.unshift( function ( d ) {
return lookup[ d ] !== undefined ?
name :
null;
} );
// Add sorting method
types.order[ name+'-pre' ] = function ( d ) {
return lookup[ d ];
};
};
})(jQuery);

Loading…
Cancel
Save