Sort datatable only by reversing the order of its elements?

Sort datatable only by reversing the order of its elements?

mallulumallulu Posts: 3Questions: 1Answers: 0

Hello.

I have an ordered table with the following column definitions:

columns: [
{ data: "FromUser", title: vis_tra_from, width: "100px", orderable: false},
{ data: "ToUser", title: vis_tra_to, width: "100px", orderable: false},
{ data: "CorresPlace", title: resources_correspondencePlace, width: "100px", orderable: false},
{ data: "subject", title: nonar_instructions, width: "100px", orderable: false},
{ data: "Date", title: vis_tra_date, width: "50px"},
{ data: "Purpose", title: vis_tra_goul, width: "300px", orderable: false}
]

As you can see from the definition, I disabled all ordering except on the column of index 4. Now, due to the nature of the format of the date in column(4), sorting it yields incorrect sorted results. However, the data itself is already sorted in ascending order by date, when pulled from our database, so in reality, the DataTable is always initially sorted in ascending order. So, functionally, if I wanted to view it in descending order, I'd only need to reverse the order of the elements.

Is there a way in which the order functionality on column(4) can be overridden to simply reverse the order of the table rows, when the column header is clicked?

Answers

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    See this blog about the recommended way to sort dates.

    Kevin

  • mallulumallulu Posts: 3Questions: 1Answers: 0
    edited September 2021

    I have gone through the blog post for the moment.js plugin, and have tried it out, but it did not manage to provide the solution I wanted. The string format for dates is as so: 13:38:51 | 1443-1-14, i.e. its moment.js format would be: HH:mm:ss | YYYY-D-MM.
    However, calling $.fn.dataTable.moment('HH:mm:ss | YYYY-D-MM') right before the declaration of the DataTable does not seem to read the format of the data as such, which is why I am currently unable to implement sorting normally on it.

  • mallulumallulu Posts: 3Questions: 1Answers: 0

    My apologies, I was not aware that moment.js would not be able to read years before 1970. My date is using Hijri years. I used the plugin moment-hijri.js and changed my moment formatter to $.fn.dataTable.moment('HH:mm:ss | iYYYY-iD-iMM'), and it worked out; I can now sort dates normally.

Sign In or Register to comment.