chris chris - 6 days ago 6
jQuery Question

jquery datatables column not sorting properly

I think I know the issue with this, but I do not know how to approach it properly so I am hoping someone here has had a similar issue and managed to fix it some how. What I have is a table with a few columns all of which work as far as sorting goes except one. below is a screen capture of that column and its sorting at work.

enter image description here

as you can see it does not sort according to alpha-numeric logic. My assumption is that some of the names have characters in them such as comma's parentheses, brackets, and so on. So that said, how would I tackle this issue so I can sort this alpha-numericly using the datatables plugin? Idea's?

****EDIT****

This is the code I am working with, works for everything but this one column..

jQuery.fn.dataTableExt.oSort['num-asc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};

jQuery.fn.dataTableExt.oSort['num-desc'] = function(a,b) {
var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};

$(document).ready(function() {
$('#ledger').dataTable({
bAutoWidth: false,
bJQueryUI : true,
bProcessing: true,
bServerSide: false,
sPaginationType: "full_numbers",
bStateSave : false,
bUseRendered: false,
iDisplayLength: ${entriesValue},
sDom: mw.superadmin.datatable.relatedListDom,
aLengthMenu: mw.superadmin.datatable.relatedListLengthMenu,
aaSorting: [[0,'asc']],
aoColumns: [
null,
{ "iDataSort": 2},
{ "bVisible": false, "sType": "num"},
{ "iDataSort": 4, "bSortable": true },
{ "bVisible": false, "sType": "num"}
]
});

Answer

Since you're showing a link, I'm guessing you're using a custom render function?

If that's the case, in your column definition set the following:

"bUseRendered": false

That'll make the table sort on the data, and not the rendered output.

Comments