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
How to impose numerical sort with jQuery and dataTables?
提问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.93
is shown after -631
and -456
. How can I make DataTable
sort a column numerically?
我正在使用 DataTables jQuery 插件。我正在尝试启用排序交互,但是在排序时按字母顺序而不是数字顺序排序。正如您在所附图片中看到的那样,-4317.93
显示在-631
和之后-456
。如何DataTable
按数字对列进行排序?
回答by Rory McCrossan
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,您需要设置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 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