javascript - Explicit Sorting for datatables -


i have column in datatable contains status information (i.e. good, good, ok, bad, bad), , i'd sort status, not alphabetically. how do that?

i made property, there this?

$("#mytable").datatable({     "sortarray": ['very good', 'good', 'ok', 'bad', 'very bad'] }); 

i did through datatables documentation , found custom sorting, uses types such alphabetic , numeric, far can tell.

edit: can't use html5 because must compatible ie9.

solution 1

you can use columndefs target specific column using zero-based index in targets option , columndefs.render return numerical value during sorting (type === 'sort'). jquery helper method $.inarray() used return array index based on value provided.

$('#mytable').datatable( {     "columndefs": [         {             "targets": 0,             "render": function ( data, type, row, meta ) {                 if(type === 'sort'){                    var values = ['very good', 'good', 'ok', 'bad', 'very bad'];                    data = $.inarray(data, values);                 }                  return data;             }         }     ] } ); 

solution 2

alternative solution use data- attributes shown in html5 data-* attributes example.

if have static html data, create rows shown below, jquery datatables picks data- attributes , uses them searching.

<tr>     <td>t. nixon</td>     <td>system architect</td>     <td data-order="1">very good</td> </tr> <tr>     <td>t. nixon</td>     <td>system architect</td>     <td data-order="2">good</td> </tr> 

Comments