如何使用jQuery和dataTables进行数字排序?

时间:2022-05-09 14:23:37

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?

我正在使用DataTables jQuery插件。我正在尝试启用排序交互,但在排序时,它按字母顺序排序,而不是数字排序。如附图所示,-4317.93显示在-631和-456之后。如何使DataTable以数字方式对列进行排序?

如何使用jQuery和dataTables进行数字排序?

1 个解决方案

#1


17  

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:

使用最新版本的DataTables,您需要在columnDefs数组中设置您提供的对象的type属性,如下所示:

$('#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.

您需要将sType参数添加到列定义中。

For example:

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

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

DataTable文档中的更多信息:http://www.datatables.net/plug-ins/sorting

#1


17  

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:

使用最新版本的DataTables,您需要在columnDefs数组中设置您提供的对象的type属性,如下所示:

$('#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.

您需要将sType参数添加到列定义中。

For example:

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

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

DataTable文档中的更多信息:http://www.datatables.net/plug-ins/sorting