How to sort data on currency basis in datatable

Posted By : Shiv Kumar | 01-Jul-2014

I had a requirement to sort the currency in ascending and descending order while using datatables. I have sort out this problem by using the following code in my javascript file.

Code in javascript file where you have used the datatable :

jQuery.extend(jQuery.fn.dataTableExt.oSort,{
    "currency-pre": function (a) {
        a = (a === "-") ? 0 : a.replace(/[^\d\-\.]/g, "");
        return parseFloat(a);
    },
    "currency-asc": function (a, b) {
        return a - b;
    },
    "currency-desc": function (a, b) {
        return b - a;
    }
});
 

The above code snippet works on the given column by calculating the difference between the value in the column.

During datatable initialisation we have to give the target column to apply sorting like this :

var oTable = $('#table_ID').dataTable({
		 'aoColumnDefs': [
		        { 'sType': 'currency', 'aTargets': [4] }     // In this case 5th column will be sorted on currency basis.
		     ],
	        "bProcessing": true,
	        "sPaginationType": "full_numbers",
		"bDestroy": true,
		"bStateSave" : true,     
     });	    
} 

In the similar way we have also some in-built functions to sort column data on different basis.

 

Thanks

Shiv Kumar

About Author

Author Image
Shiv Kumar

Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.

Request for Proposal

Name is required

Comment is required

Sending message..