How to impose numerical sort with jQuery and dataTables?

后端 未结 1 1602
陌清茗
陌清茗 2021-02-19 06:48

I am using the DataTables jQuery plugin. I am trying to enable sort interaction, but when sorting it sorts alphabetically and not numerically. As you can see in the enclosed pic

相关标签:
1条回答
  • 2021-02-19 07:33

    Updated answer

    With the latest version of DataTables you need to set the type property of the object you provide in the columnDefs array, like this:

    $('#example').dataTable({
      "columnDefs": [
        { "type": "num" }
      ]
    });
    

    Note that there are many other methods of sorting which can be found in the documentation


    Original answer

    You need to add the sType parameter to your column definition.

    For example:

    $('#example').dataTable({
      "aoColumnDefs": [
        { "sType": "numeric" }
      ]
    });
    

    More information in the DataTable documentation: http://www.datatables.net/plug-ins/sorting

    0 讨论(0)
提交回复
热议问题