Date sorting with moment.js. Empty cells to bottom, please.
Date sorting with moment.js. Empty cells to bottom, please.
I'm using the moment.js-plugin for sorting localized dateformats and it works great. My only gripe is that empty cells are listed before cells with a value when the relevant column is sorted in descending order. Implementing a custom sorting-function seems to be the only way to achieve this. Unless there's some config-option I've missed, this seems to be an overly difficult and complicated way just to achieve that particular behaviour. Compare this to jQuery tablesorter 2.0 which has the very convenient option {emptyCellsTo: 'bottom'}
.
I haven't considered writing a custom sorting function, but how would it combine with the moment.js sorting-plugin if I decided to write one? Does the community have any other suggestions? Isn't it about time that something similar to tablesorter 2.0's solution be added to DataTables?
Thanks in advance!
This question has an accepted answers - jump to answer
Answers
No one has anything on this?
Sounds like a good idea. I've never seen that option before - but I do like it and I've add it to my list.
In the mean time, you could modify this file to allo the auto detect to accept empty strings as a valid value for that type. Oddly the sorting function itself does already handle an empty string as -Infinity (i.e. bottom), but the auto detect isn't allowing it to get that far.
Allan
I'm delighted to see that you still inhabit your own forum, Allan, active as ever
So that's why modifying the moment-sorter didn't work. I'll have a looksie later and get back to you.
I think it'd be a useful feature to have, and I'm glad you agree.
Thanks!
I think that you did your job well when you wrote the moment-plugin back in 2014. The type detection code includes these lines:
... which would seem to indicate that empty - or null values are indeed treated as valid for that type....... wouldn't it? I'm not sure modifying the type detection is the solution here. If I'm reading the code correctly, the sort function converts my dates to a unix timestamp and any given unix timestamp will always be more than -Infinity. So, if I sort my dates in ascending order, wouldn't it make sense that the empty rows (which are treated as -Infinity) come before those with a value? That is precisely the behaviour I'm seeing now.
Edit:
In case it wasn't clear in my OP, I want empty cells to be at the bottom no matter which way the date-column is sorted, i.e. they're ignored. I don't know how to accomplish this in the moment sorting-function. I mean, which value could it return so that the relevant row is sent to bottom no matter what?
Thanks
That is in the sorting part of that plug-in.
A few lines above is where it adds its type detection. There it accepts only a valid Moment date. That should be modified to check for empty string or null as well and allow that to be valid.
Ah - no I didn't get that. That would require a different sorting method in that case. Rather than having a single
-pre
sorting function, you'd need to provide both a-asc
and-desc
sorting function that would each sort the empty cells to the bottom.There documentation here will explain that terminology.
So yes, not quite as simple as I had thought, but still quite do-able.
Allan
No, it is not. Here is the same code + the final return statement from the type detection part.
Yeah, it's as my research indicated initially then; a custom sorting function is needed. That's why I mentioned jQuery Tablesorter 2's emptyCellsTo-option. It works the way I want and from the dev's point of view, it's simple and easy to use. Hopefully it will be part of DataTables' repertoire in the future as well
Thanks for your help, Allan.
Yup - I think that would be a useful addition. Thanks!
For future reference and/or anyone wondering, this is how I ended up doing it in my case:
Nice one! Thanks for sharing that with us!
Allan