How to impose numerical sort with jQuery and dataT

2019-04-04 09:16发布

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 picture, -4317.93 is shown after -631 and -456. How can I make DataTable sort a column numerically?

Example

1条回答
Juvenile、少年°
2楼-- · 2019-04-04 09:52

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

查看更多
登录 后发表回答