Sorting with Number not working

Sorting with Number not working

arunselvaguruarunselvaguru Posts: 4Questions: 0Answers: 0
edited June 2012 in DataTables 1.9
Hi,
I am using Jquery Datatables for sorting. I have a field called Capacity. While sorting it sorts as string.

Here is my code:

$(document).ready(function () {
$('#grdCentre').dataTable({
"aoColumns": [{ "bSortable": false }, null, null, null, { "sType": "numeric" }, null]
});

$(".dataTables_length").hide();
$(".dataTables_filter").hide();
});

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    There much be non-numeric data in the column in which case. You might need a sorting plugin: http://datatables.net/plug-ins/sorting

    Allan
  • arunselvaguruarunselvaguru Posts: 4Questions: 0Answers: 0
    Allan, Thank you for your quick response. Can you let me know where to download the dataTables.numericComma.js and dataTables.dataSourcePlugins.js.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    On the page I linked to: http://datatables.net/plug-ins/sorting#numeric_comma

    Allan
  • arunselvaguruarunselvaguru Posts: 4Questions: 0Answers: 0
    Allan,
    I have tried the method and still not able to sorting.
    Here is my code:


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

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

    $(document).ready(function() {
    $('#grdCentre').dataTable({
    "aoColumns": [
    null,
    null,
    null,
    null,
    { "sType": "numeric-comma" },
    null
    ]
    });
    });


    Let me know if I have to do any changes
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Looks okay to me :0). Perhaps you can run your table through the DataTables debugger and give me the unique code so I can see what might be happening.

    Allan
  • arunselvaguruarunselvaguru Posts: 4Questions: 0Answers: 0
    Allan,
    Thank you for your input. I have changed the code like this



    <%--





    --%>

    Now its working fine.

    Reason:
    When I use the TemplateField , it change the value to string with data. So i have changed TemplateField to BoundField and its not coming with data. Finally its working as expected.

    Thank you for your feedback.

    -ArunS
This discussion has been closed.