RSS

Sorting formatted numbers in jquery datatable

11 May

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);
};

Advertisements
 
Leave a comment

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

 

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: