如何使用jQuery和dataTables进行数字排序? [英] How to impose numerical sort with jQuery and dataTables?

查看:493
本文介绍了如何使用jQuery和dataTables进行数字排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



解决方案

更新回答



使用最新版本的DataTables,您需要设置<$ c中提供的对象的类型属性$ c> columnDefs 数组,如下所示:

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

请注意,还有许多其他排序方法可以在文档






原始回答



您需要在列中添加 sType 参数例如:

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

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


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?

解决方案

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

这篇关于如何使用jQuery和dataTables进行数字排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆