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?
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?
Share Improve this question edited Sep 19, 2015 at 15:04 Gyrocode. 58.9k16 gold badges156 silver badges191 bronze badges asked Sep 18, 2015 at 12:40 LAffairLAffair 1,9986 gold badges33 silver badges64 bronze badges1 Answer
Reset to default 10SOLUTION
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.