javascript jQuery DataTables:如何按自定义参数值而不是单元格内容排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13780739/
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 19:45:18  来源:igfitidea点击:

jQuery DataTables: how to sort by custom parameter value rather than the content of the cell?

javascriptjqueryhtmldatatabledatatables

提问by Sherzod

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:

我有一个非常常见的用例,我在价格列中显示格式化的价格,例如。“20,000.00”。因此,当我尝试对其进行排序时,它会将其视为字符串并且排序不好:

  • 10.00
  • 20,000.00
  • 5,000.00
  • 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?

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

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

与这个问题相关:如何禁用给定列的排序?我正在使用数据表 1.9.4。

回答by GameCharmer

To answer your first question, you could use the Formatted Numbers plugin available on the DataTables plugin page. I would post the code here, but since they update often, I'll just post the link instead.

要回答您的第一个问题,您可以使用 DataTables 插件页面上提供的 Formatted Numbers 插件。我会在这里发布代码,但由于它们经常更新,我只会发布链接。

http://datatables.net/plug-ins/type-detection

http://datatables.net/plug-ins/type-detection

You have a couple of options for disabling sorting on a particular column. You could take the legacy route and put a line in your init object such as...

您有几个选项可以禁用对特定列的排序。您可以采用传统路线并在您的 init 对象中放置一行,例如...

"aoColumns": [
   null,null,null,{ "bSortable": false },null,null
]

Where null is a column you don't want to do anything to, and the bSortable object is the column you want to effect.

其中 null 是您不想对其执行任何操作的列,而 bSortable 对象是您想要影响的列。

Since you are running 1.9+, you can do the following.

由于您运行的是 1.9+,因此您可以执行以下操作。

"aoColumnDefs": [
    { "bSortable": false, "aTargets": [ 4 ] }
],

In this example, 4 is the column you want to disable sorting on. Remember, the first column is 0, so this would technically be the 5th column.

在此示例中,4 是您要禁用排序的列。请记住,第一列是 0,因此从技术上讲,这将是第 5 列。

回答by David Stetler

Use this page http://datatables.net/plug-ins/sortingto 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.

使用此页面http://datatables.net/plug-ins/sorting查看您可以添加的所有排序类型。那里有很多并且易于使用。基本上,您需要包含它在每种类型的显示详细信息部分下显示的代码段。需要在数据表初始化后包含此代码。就我个人而言,因为我在我的网站上使用了很多,所以我制作了一个名为 datatables.sorting.js 的单独文件,并在包含数据表之后包含该文件。通过这种方式,我可以根据需要添加尽可能多的各种排序类型。

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

添加代码后,您可以使用 aoColumns 参数告诉数据表将该排序方法应用于您想要的任何列:

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

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

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.

我添加了用于对格式化数字进行排序的代码,您可以像我一样将其包含在内,也可以将其包含在单独的文件中。