Datetime ordering with moment.js order column alphabetically
Datetime ordering with moment.js order column alphabetically
Hi, i have only this last problem to make DataTable works for my asp.net website.
I start from this topic: https://datatables.net/blog/2014-12-18
and download moment.js and datetime-moment.js and add them to my page
this is the start of initialization code:
<script type="text/javascript">
$(Document).ready(function () {
$.fn.dataTable.moment('dd/MM/YYYY');
$.fn.dataTable.moment('L');
var table = $('#tblCruscottoDaAvviare').DataTable(
{
"scrollY": "300px",
"scrollX": true,
"paging": false,
"order": [],
dom: 'Brt',
buttons: [
......
From DataTable point of view table is static (html plain code in generatd my asp.net mvc razor code)
Date are in italian format: dd/MM/YYYY /for example 25/12/2017
Now when i order column with datetime, alphabetical order is used, so 01/12/2017 comes before 02/01/2010
i have to add other things that i miss?
this is the debug link:http://debug.datatables.net/amacoc
looking ot it i see that type recognition go wrong for some column:
DataStatus is data but find string
DataFinePrevista is data but find html
StatoAvanzamento is text (now all empty string) but find moment-L
SocietaService is text but find moment-L
DataAggiornamentoSAL is the only data column render correctly as moment-L but all row are empty so how can type be recognized as date?
thanks
This question has an accepted answers - jump to answer
Answers
Looks like you are overwriting the first setting with the latter:
if you use L you need to specify a locale for moment
Consider this (I set moment.locale but also define a variable momentLocale to be able to send it to data tables. I also need this in the data table definitions.)
This works fine for me. Don't forget the moment locale files.
This is how I use momentLocale in the table definitions:
was a test, i can leave only the first without using locale (i have only italian format to manage)
column are variable and not fixed (datasource can change and table is dinamically adatpted) so i can't use 'fields: ' to specify format
If that is the Italian format 25/12/2017 and you are using this $.fn.dataTable.moment('dd/MM/YYYY') then it is obviously not the same!
This is from the italian moment locale file:
from the moment.js website:
use $.fn.dataTable.moment('DD/MM/YYYY'), no need to tuse the locale files then.
was so simple but great idea looking at italian locale
in many other language 'd' is for days and not 'D'...
thank you very much