I notices that UK date sorting does not work when a date is also a link.
Example 1. (demo)
Here the date is pure test. Works complete fine.
<tr>
<td>01/01/01</td>
<td>Tarik</td>
<td>Rashad Kidd</td>
<td>1 34 238 6239-0509</td>
</tr>
Example 2. (demo)
Here the date is also a link. Does not work at all. Not throwing any errors though.
<tr>
<td><a href="#">01/01/01</a></td>
<td>Tarik</td>
<td>Rashad Kidd</td>
<td>1 34 238 6239-0509</td>
</tr>
I also noticed that the sorting does work on any other elements even if they are a link. Only the date as a link are the issue.
I'm using the following JS code:
// UK Date Sorting
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
$(document).ready(function() {
$('#table').dataTable( {
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"aoColumnDefs" : [
{ "aTargets" : ["uk-date-column"] , "sType" : "uk_date"}
]
});
});
Any help much appreciated.
As stated here you need to include Moment. js and the datatable-moment plugin, then just declare the date format you are using. The plugin will autodetect your date columns and sort it like it should be.
date format should be YYYY-MM-DD. sort it *"order": [[ 3, "desc" ]] * and hide the td, th display none.
DataTable allows you to sort data rows on the client side. There are 2 ways to invoke sorting in the table: By a single click on the header of a column with the enabled sort attribute; By API call ( can be called from some event or action, i.e button click or page load ) of the sort() method.
The default behavior of DataTables allows the sorting of multiple columns at one time by holding the Shift key and clicking on the header cells in the order that needs to be sorted.
The problem is that you sorting function is confused by the extra html. You should modify your functions like this:
// UK Date Sorting
jQuery.fn.dataTableExt.oSort['uk_date-asc'] = function(a,b) {
//use text()
var ukDatea = $(a).text().split('/');
var ukDateb = $(b).text().split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function(a,b) {
//use text()
var ukDatea = $(a).text().split('/');
var ukDateb = $(b).text().split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
fiddle here http://jsfiddle.net/GUb2n/
You can try to put the date (in ISO Format) in an invisible container in front of the link:
<span style="display: none;">2001-01-01</span><a href="#">01/01/01</a>
Then alphabetic sort should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With