jQuery DataTables - 按隐藏列排序日期 [英] jQuery DataTables - Ordering dates by hidden column
问题描述
我已经使用 DataTables 工作了几天,我有这个任务:我需要禁用初始排序并过滤包含 Aug 15
之类的日期的第一列,具体取决于第四列 (2015.08.15
),这将被隐藏.
例如,如果我有:
8 月 15 日 |2015.08.158 月 7 日 |2015.08.078月3日|2015.08.038 月 20 日 |2015.08.20
在升序中我应该得到:
8 月 3 日 |2015.08.038 月 7 日 |2015.08.078 月 15 日 |2015.08.158 月 20 日 |2015.08.20
但我得到了字母排序:
8 月 15 日 |2015.08.158 月 20 日 |2015.08.208月3日|2015.08.038 月 7 日 |2015.08.07
我的第一个代码是这样的:
$("#TableBt" +rid).DataTable({"asorting": [],列": [空值,空值,{标题":LC2},{可见":假}]
这禁用了我的初始排序,但它按字母顺序对我的日期列(第一个可见的列)进行了排序.
经过一番研究,我把代码改成这样:
$("#TableBt" +rid).dataTable({"asSorting": [],aoColumnDef":[{"iDataSort": 3,目标":[4]},空值,{标题":LC2},{bVisible":假,目标":[3]}]});
但现在所有列都可见,再次启用初始排序并且日期排序仅按字母顺序工作.
我做错了什么?
解决方案
您需要使用 columnDefs
来定位第一列(targets: 0
) 并使用 orderData
.您还需要使用 visible: false
隐藏第四列 (targets: 3
).
$("#TableBt" +rid).DataTable({列定义:[{目标:0,订单数据:3},{目标:3,可见:假}]});
<块引用>
演示
有关代码和演示,请参阅此 jsFiddle.
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.
For example, if I have:
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
But I get the alphabetical sort:
Aug 15 | 2015.08.15
Aug 20 | 2015.08.20
Aug 3 | 2015.08.03
Aug 7 | 2015.08.07
My first code was something like:
$("#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.
What am I doing wrong?
SOLUTION
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
See this jsFiddle for code and demonstration.
这篇关于jQuery DataTables - 按隐藏列排序日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!