jQuery DataTables:如何按照自定义参数值排序而不是单元格的内容? [英] jQuery DataTables: how to sort by custom parameter value rather than the content of the cell?

查看:151
本文介绍了jQuery DataTables:如何按照自定义参数值排序而不是单元格的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很常见的用例,我在价格列中显示格式化的价格,例如。 20,000.00。所以当我尝试排序时,它将它视为一个字符串,排序不佳:

I have a pretty common use case where I show the formatted price in a Price column, eg. "20,000.00". So when I try to sort it, it treats it as a string and doesn't sort well:


  • 10.00

  • 20,000.00

  • 5,000.00

我可以让它排序通过数据参数值,这将是非格式的浮点数?

Can I make it so that it would sort by the data- parameter value, which would be non-formatted float number?

与此问题相关:如何禁用给定列的排序?我正在使用DataTables 1.9.4。

And related to this question: how do you disable sorting for the given column? I'm using DataTables 1.9.4.

推荐答案

使用此页 http://datatables.net/plug-ins/sorting 以查看可以添加的所有排序类型。有很多在那里,很容易使用。基本上你需要包括它显示在每个类型的显示详细信息部分下的代码。在初始化数据表之后,需要包含此代码。由于我有相当多的我使用我的网站,我已经制作了一个单独的文件,名为datatables.sorting.js,我包括在我包含数据。这样我可以根据需要添加多种排序类型。

Use this page http://datatables.net/plug-ins/sorting to see all the sorting types you can add. There are quite a few out there and are easy to use. Basically you need to include the piece of code that it shows under the show details section of each type. This code needs to be included after datatables has initialized. Personally since I have quite a few that I use across my site, I have made a separate file called datatables.sorting.js and I include that after I include datatables. This way I can add as many of the various sorting types as needed.

添加代码后,您可以使用aoColumns参数来告知数据表,以便将该排序方法应用于所需的列:

After you add the code, you can use the aoColumns parameter to tell datatables to apply that sorting method on whichever column you want:

$('#myTable').dataTable({
"aoColumns": [
            null,
            null,
            { "sType": "formatted-num" }
        ]
});

http://jsfiddle.net/davidstetler/5Z8fZ/

我添加了排序格式化数字的代码,您可以像我一样包含,或者您可以将其包含在单独的文件中。

I added the code for sorting formatted numbers which you can include like I did, or you can include in a separate file.

这篇关于jQuery DataTables:如何按照自定义参数值排序而不是单元格的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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