jQuery DataTables - 按隐藏列排序日期 [英] jQuery DataTables - Ordering dates by hidden column
问题描述
我已经使用DataTables工作了几天而且我有这个任务:
我需要禁用初始排序并过滤包含日期的第一列,例如 8月15日
取决于第四个( 2015.08.15
),这将被隐藏。
I have been working for a couple of days with DataTables and I have this task:
I need to disable the initial sorting and filter the first column which contains dates like Aug 15
depending on the fourth one (2015.08.15
), which will be hidden.
例如,如果我有:
Aug 15 | 2015.08.15
Aug 7 | 2015.08.07
Aug 3 | 2015.08.03
Aug 20 | 2015.08.20
按升序排序我应该得到:
In ascending sort I should get:
Aug 3 | 2015.08.03
Aug 7 | 2015.08.07
Aug 15 | 2015.08.15
Aug 20 | 2015.08.20
但我按字母顺序排序:
Aug 15 | 2015.08.15
Aug 20 | 2015.08.20
Aug 3 | 2015.08.03
Aug 7 | 2015.08.07
我的第一个代码如下:
$("#TableBt" + rid).DataTable({
"aaSorting": [],
"columns": [
null,
null,
{
"title": lC2
},
{
"visible": false
}]
这禁用了我的初始排序,但它按字母排序我的日期列(第一个和可见的)。
This disabled my initial sorting, but it alphabetical sort my date column (the first and visible one).
经过一番研究,我改变了这样的代码:
After some research, I changed the code like this:
$("#TableBt" + rid).dataTable({
"asSorting": [],
"aoColumnDef": [
{
"iDataSort": 3,
"aTargets": [4]
},
null,
{
"sTitle": lC2
},
{
"bVisible": false,
"aTargets": [3]
}]
});
但现在所有列都可见,初始排序再次启用,日期排序仅按字母顺序排列。
But now all the columns are visible, the initial sorting is again enable and the date sort works only alphabetical.
我做错了什么?
推荐答案
解决方案
您需要使用 columnDefs
定位第一列( targets:0
)并定义用于排序第一列的数据列 orderData
。此外,您需要隐藏列(目标:3
),其中 visible:false
。
You need to use columnDefs
to target first column (targets: 0
) and define the column which data would be used for sorting of the first column with orderData
. Also you need to hide forth column (targets: 3
) with visible: false
.
$("#TableBt" + rid).DataTable({
columnDefs: [
{ targets: 0, orderData: 3 },
{ targets: 3, visible: false }
]
});
DEMO
有关代码,请参见此jsFiddle 和示范。
See this jsFiddle for code and demonstration.
这篇关于jQuery DataTables - 按隐藏列排序日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!