multiple columndefs with moment date format
multiple columndefs with moment date format
Lapointe
Posts: 430Questions: 81Answers: 4
I read a link sample here (from colin): http://live.datatables.net/nizanuzo/24/edit
$(document).ready(function () {
$.fn.dataTable.moment('M/D/YYYY h:mm a');
$('#tblDataTable').DataTable({
paging: false,
columnDefs: [{targets: [0, 3, 5] }]
});
});
$.fn.dataTable.moment('M/D/YYYY h:mm a'); is not set explicitly for columns else using targets...
So, how to mix this columndefs target setting like
$('#tblDataTable').dataTable( {
"columnDefs": [ {
"targets": [ 2, 4 ],
"orderable": false
},{
"targets": [0, 1, 2],
"searchable": false
}, {
targets: [0, 3, 5]
//** to be linked with date moment setting (use sort plugins)**
//** $.fn.dataTable.moment('M/D/YYYY h:mm a');**
} ] } );
Another one question...
target vs targets is usable in both cases?
In colin sample, target was used for columnsdefs, in documentation targets for columnsdefs and target for responsive details
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
No. As you noted the
columnDefs.targets
requires thes
meaning the option istargets
. The example you linked to has a typo withtarget
. But also note thetype
option is commented out.Generally you don't need to set anything in the
columns
orcolumnDefs
to apply the moment plugin. As described in thecolumns.type
docs Datatables has some built in types that it will automatically detect. If the all the data in the column does not match one of these types it defaults to string. You can use the moment sorting plugin when your date formats doesn't match the builtin date formats.The format (
'M/D/YYYY h:mm a'
) you use needs to match the format of the dates in the column. If it doesn't then the column will be sorted as a string. I took the example you linked to and changed one of the dates from6/19/2018 11:57 am
to06/19/2018 11:57 am
. This change doesn't match the defined format of `'M/D/YYYY h:mm a'. The column is now not sorted by dates but as a string. Here is the example:http://live.datatables.net/bayisaji/1/edit
The short version is that 'M/D/YYYY h:mm a' is likely not matching the format in the column if its not sorting correctly by datetime. You will need to look at the moment docs to create the proper format.
Kevin
thanks @kthorngren
I did understand what you wrote else the fact that there is no reason to set targets for use moment sort option...
Is it correct ?