RSS

Monthly Archives: May 2012

Sorting formatted numbers in jquery datatable

We can sort formated numbers in jquery data table. Here is the code. If we initialize our jquery data table by class ‘datatable’ and the 5th column contains formatted numbers data( comma separated numbers) then we can use the following code.

$(‘.datatable’).dataTable( {
“aoColumns”: [
null,
null,
null,
null,
{ “sType”: “formatted-num” }
]
});

jQuery.fn.dataTableExt.oSort[‘formatted-num-asc’] = function(a,b) {
/* Remove any formatting */
var x = a.match(/\d/) ? a.replace( /[^\d\-\.]/g, “” ) : 0;
var y = b.match(/\d/) ? b.replace( /[^\d\-\.]/g, “” ) : 0;

/* Parse and return */
return parseFloat(x) – parseFloat(y);
};

jQuery.fn.dataTableExt.oSort[‘formatted-num-desc’] = function(a,b) {
var x = a.match(/\d/) ? a.replace( /[^\d\-\.]/g, “” ) : 0;
var y = b.match(/\d/) ? b.replace( /[^\d\-\.]/g, “” ) : 0;

return parseFloat(y) – parseFloat(x);
};

 
Leave a comment

Posted by on May 11, 2012 in jQuery, JS Plugins

 

Tags: , ,