Sorting dates in columns
Sorting dates in columns
I see this: https://datatables.net/reference/option/columns.type
From what I understand, effectively a date column can only be sorted provided they are displayed as YYYY-MM-DD?
Is that right? The "limited set of dates" link to in the option shows YYYY-MM-DD ... YYYY/MM/DD
Which effectively is just sorting numerically and doesn't really require the field type to be a date? If I put the date into a format that allows sorting numerically, there is no point to defining a column type = date?
As can be seen, http://live.datatables.net/lidequra/21/edit - all date columns are displayed DD/MM/YY and don't sort
Am I misinterpreting the doco? Displaying the date as YYYY-MM-DD is really unfriendly and I want to avoid it?
This question has an accepted answers - jump to answer
Answers
Check:
https://datatables.net/plug-ins/dataRender/datetime
It Man
Hello @sjw01
When you're using the DataTables date column you are limited to a very finite subset of formatting options, all of which I'm pretty sure, are in the format YYYY-MM-DD.
If you are wishing to have a user friendly format, I'd recommend using the
render
attribute of the column declaration in your table initiation, to render an HTML5 Input of type: date (<input type="date" name="inputFieldName">
) in the editor and pushing the information around yourself.In other news, I've had a play with the example and the date columns sort fine??
Have a read over this blog post which goes into full details of date / time sorting in DataTables.
Allan
@MSLtd It would appear that the column is sorted using the rendered data so we're back to square one. The columns definitely don't sort as required.
@allan thanks, i'll take a look
I read over the blog post and I implemented moment exactly as described.
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script type="text/javascript" src="//cdn.datatables.net/plug-ins/1.10.19/sorting/datetime-moment.js"></script>
{ title: "RO Date", type: "date" },
$.fn.dataTable.moment( 'DD/MM/YY' );
Still wont sort date columns ???
http://live.datatables.net/lidequra/30/edit
You had:
Remove the
type
option. That is incorrectly forcing the data type. Allow DataTables to auto-detect the type.Allan