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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8008442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:58:23  来源:igfitidea点击:

How to impose numerical sort with jQuery and dataTables?

javascriptjquerydatatables

提问by Spredzy

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.93is shown after -631and -456. How can I make DataTablesort a column numerically?

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

Example

Example

回答by Rory McCrossan

Updated answer

更新答案

With the latest version of DataTables you need to set the typeproperty of the object you provide in the columnDefsarray, like this:

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

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