wawanopoulos
wawanopoulos

Reputation: 9804

Jquery datatables: sort number with comma doesn't work

I am trying to sort a column with numerical numbers with comma (,).

I am getting a wrong result using the num-fmt option:

enter image description here

Here is my code :

$('#test').DataTable({
    columnDefs: [
         { targets: 4, type: 'num-fmt' }
    ]
});

Upvotes: 2

Views: 3200

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58900

Use numeric-comma plugin to sort numbers correctly which use a comma as the decimal place.

Either include //cdn.datatables.net/plug-ins/1.10.11/sorting/numeric-comma.js or use it inline as shown below:

$.extend( $.fn.dataTableExt.oSort, {
    "numeric-comma-pre": function ( a ) {
        var x = (a == "-") ? 0 : a.replace( /,/, "." );
        return parseFloat( x );
    },

    "numeric-comma-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "numeric-comma-desc": function ( a, b ) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );

$('#test').DataTable({
    columnDefs: [
         { targets: 4, type: 'numeric-comma' }
    ]
});

Upvotes: 6

Related Questions