Datatables sort type asc / desc
Datatables sort type asc / desc
Hi,
Link to the test case : https://live.datatables.net/xawerapu/1/edit
To explain the situation : I have a date at the format (DD/MM) that i transform to the format MM/DD when sorting to get it sort well but still display at the format (DD/MM) -> that work as intended.
The problem that i m facing is there is also empty value in that column. I want to sort those null value in last position in both case (sort asc / sort desc) but it look like it doesn't work -> still sort first when asc. Can you explain what i m doing wrong ?
After some test, for me it look like if (type === "asc") and if (type === "asc") doesn't work...
Thank ![]()
This question has an accepted answers - jump to answer
Answers
It wouldn't - the
typeparameter is never passed those values for the rendering function. Indeed, further up you havetype === 'sort'which will evaluate to true, meaning that a check for a different value oftypein that condition will always befalse!What you need is a sorting plug-in - they handle the
ascanddesccases.Have a look at this blog post which basically does what you need. Just have your rendering function return a number for the
type === 'sort'case (or null) and use theabsoluteOrderoption described in that post.Allan
ok it work with the absolute plugin for the above case thx !
Now i have another question, is it possible to use the absolute plugin when the column have date as full format ( DD/MM/YYYY) and also emty value :
i use this but it doesn't work, null value go to the bottom everytime (ok) but the date are not sorting correcty ( they are sorting by DD and not the whole date anymore DD/MM/YYYY) :
I would convert the date to a timestamp so its a number. I updated your example to use moment.js to convert
DD/MM/YYYYdates to a timestamp. Also added the absolute.js plugin.https://live.datatables.net/xawerapu/2/edit
Kevin
this could work BUT i will lose the searchbuilder calendar because the column is not a datetime anymore and i realy need this feature...
It's always something.
After a bit of research I found that the Ultimate sorting plugin returns a
columns.typeof'moment-'+format:This SearchBuilder example indicates that the
columns.typewill bemoment.I took the absolute.js plugin code and defined the name as
moment-DD/MM/YYYYwhich is what is returned for thecolumns.type.SearchBuilder now uses the date picker for the date column. Here is the updated example:
https://live.datatables.net/xawerapu/3/edit
The plugin code is at the top of the Javascript tab and commented out the
scripttag to load it. The other change is I removed the.valueOf()from thecolumns.renderfunction. I usedinitCompleteoutput thesettingsto validate thecolumns.typesetting:@allan can confirm if this is a valid option.
If you use Luxon then probably something similar can be done but return the type as
luxon.Kevin
That does complicate things a bit. It might be that your best option is to modify the built in DataTables' sorting for dates to basically act like the absolute ordering plug-in.
Can you show me the current state of your page please? I.e. what data and initialisation code you are using?
Allan
My data are pretty basic : i get them from a json file, date are at the format 30/11/2023 (DD/MM/YYYY) -->
{ data: "DTBILANPREVU_SM2", name:"DTBILANPREVU_SM2", className: "editable", searchBuilderTitle: "[BILAN] Date prévue"
can you show me how to modify the built in DataTables' sorting for dates to act like the absolute ordering plug-in pls ?
i tryed ur solution @kthorngren everything look like it work but when i search for a date with searchuilder, it had a 0 at the end of my date :
i think it's because i still have this in plugin code :
because if i delete _unique+++ there is no 0 anymore but the sorting doesn't work like it should ( null value doesn't go the end). I don't understand because i have all set up like ur exemple -> i took the javascript to replace my absolute plugin code i have set up my table with those setting :
and for the column inside my table :