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

时间:2021-06-21 19:15:13

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:

我有一个非常常见的用例,我在Price列中显示格式化的价格,例如。 “20,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.

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

2 个解决方案

#1


3  

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

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列。

#2


3  

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.

使用此页面http://datatables.net/plug-ins/sorting查看您可以添加的所有排序类型。有很多,很容易使用。基本上,您需要在每种类型的show details部分中包含它显示的代码段。数据表初始化后需要包含此代码。就个人而言,因为我在我的网站上使用了很多,所以我创建了一个名为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/

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

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

#1


3  

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

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列。

#2


3  

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.

使用此页面http://datatables.net/plug-ins/sorting查看您可以添加的所有排序类型。有很多,很容易使用。基本上,您需要在每种类型的show details部分中包含它显示的代码段。数据表初始化后需要包含此代码。就个人而言,因为我在我的网站上使用了很多,所以我创建了一个名为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/

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

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