DataTables - Sort HTML integer values
HTML
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<table class="data-table">
<thead>
<tr>
<th scope="col">Specialty</th>
<th scope="col">Friday<br>21/01/2011</th>
<th scope="col">Saturday<br>22/01/2011</th>
<th scope="col">Sunday<br>23/01/2011</th>
<th scope="col">Monday<br>24/01/2011</th>
<th scope="col">Tuesday<br>25/01/2011</th>
<th scope="col">Wednesday<br>26/01/2011</th>
<th scope="col">Thursday<br>27/01/2011</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="#">ACCIDENT AND EMERGENCY</a></td>
<td><a href="#">5</a></td>
<td><a href="#">14</a></td>
<td><a href="#">67</a></td>
<td><a href="#">45</a></td>
<td><a href="#">43</a></td>
<td><a href="#">28</a></td>
<td><a href="#">36</a></td>
</tr>
<tr>
<td><a href="#">ANAESTHETICS</a></td>
<td><a href="#">36</a></td>
<td><a href="#">5</a></td>
<td><a href="#">14</a></td>
<td><a href="#">43</a></td>
<td><a href="#">28</a></td>
<td><a href="#">67</a></td>
<td><a href="#">45</a></td>
</tr>
<tr>
<td><a href="#">CARDIOLOGY</a></td>
<td><a href="#">5</a></td>
<td><a href="#">14</a></td>
<td><a href="#">67</a></td>
<td><a href="#">45</a></td>
<td><a href="#">43</a></td>
<td><a href="#">28</a></td>
<td><a href="#">36</a></td>
</tr>
...
JavaScript
jQuery.fn.dataTableExt.oSort['numeric-html-asc'] = function(a,b) {
a = parseInt($(a).text());
b = parseInt($(b).text());
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric-html-desc'] = function(a,b) {
a = parseInt($(a).text());
b = parseInt($(b).text());
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
};
$('.data-table').dataTable({
"bPaginate": true,
"bFilter": true,
"bSort": true,
"bAutoWidth": false,
"iDisplayLength": 100,
"sPaginationType": "full_numbers",
"sDom": '<"tools"T><"top"flip>rt<"bottom"lfip><"clear">',
"oTableTools": {
"aButtons": [
"copy", "csv", "xls", "pdf", {
"sExtends": "collection",
"sButtonText": "Save",
"aButtons": ["csv", "xls", "pdf"]
}]
},
"aoColumns": [
null,
{ "Type": "numeric" },
{ "sType": "numeric-html" },
{ "sType": "numeric-html" },
{ "sType": "numeric-html" },
{ "sType": "numeric-html" },
{ "sType": "numeric-html" },
{ "sType": "numeric-html" }
]
});