DataTables order Months/Weeks/Days

DataTables order Months/Weeks/Days

drdreodrdreo Posts: 6Questions: 2Answers: 0

Hello,
im working on a DataTable which contains a lot of special columns. I am stuck now with a specific form of data.
It show the Last Active time. It gets calculated from a given timestamp subtracted by the current timestamp -->last.png

I have the unix timestamp in the html data-order attribute --> unix.png

But as seen in the first image. It does not order it right. Months < Weeks. It seems it orders it alphabetically.

Any help is much appreciated.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin

    I'd need a link to the page showing the issue to be able to debug why this isn't working.

    Thanks,
    Allan

  • drdreodrdreo Posts: 6Questions: 2Answers: 0

    Hello Allan,
    thanks for your fast response.

    I can't direct you to the page im working on, because its on a dev server and data gets filled by databases and tons of linkings.

    But i recreated quickly the datasets in jsFiddle with the script i also use to exclude empty cells from sorting:
    jsFiddle Link

    Hope you can help me. I am stuck.

    Best regards,
    Andreas

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    Answer ✓

    Your table is DOM sourced so you don't need to set columns.data. It works without that option: https://jsfiddle.net/mu8d5y4c/2/ . The reason for that is that if you don't specify a value DataTables can attempt to detect the data - before it was just using the data in the cell (which the parameter effectively pointed to).

    Allan

  • drdreodrdreo Posts: 6Questions: 2Answers: 0

    Thank you very much this solved the problem with wrong sorting.

    But still a question, how can i exclude empty data cells then?
    When i've got a lot empty cells, the DESC order shows nothing but the empty cells then.

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin

    But still a question, how can i exclude empty data cells then?

    I don't understand I'm afraid. Exclude them from the table? Update whatever your data source is to exclude them before they are populated in the table (e.g. use WHERE filter if you are using SQL).

    Allan

  • drdreodrdreo Posts: 6Questions: 2Answers: 0
    edited August 2016

    Oh i am sorry. Yesterday wasn't a good day for me.
    What i've meant was, now that i removed the data specification, it sorts these data correctly. Works excellently.

    But as you can see in the jsFiddle, there is a self-written sort function "non-empty-string" in the javascript section, which is there to exclude data from being sorted that has no data but a "-" in it. For me this means the cell is empty.

    Because if there are 50 empty data-cells and i click to sort them of course the empty data will show up as the latest data, what obviously can't be. (Sort the "Last Active" column)

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin

    I still don't understand what you want to happen I'm afraid. Where should the - row go in the table when you sort the "Last Active" column? I doubt you want to return 1 or -1 without a comparison. Perhaps set the value to be infinity: https://jsfiddle.net/mu8d5y4c/3/ .

    Allan

  • drdreodrdreo Posts: 6Questions: 2Answers: 0
    edited August 2016

    "-" should not be displayed. It destroys the use of the sort function. For example i got 200 users. I want to sort the "Last Active" column when the last activity of users was.

    So i got on the one end the most recent active users and on the other end i get 100 users with "-" because those guys didn't log in once. Hence, there is no timestamp available.
    But i still want the oldest activity to be displayed if a user was 9 Months not online this should be displayed if sorted.

    This has already worked on my first JS FIddle. The one row with id=19 "Last Active" = "-" doesn't get displayed when sorted. Or if you sort "Min Trial" the "-" cells are always on the bottom. No matter ASC DESC sorted.

  • allanallan Posts: 63,281Questions: 1Answers: 10,425 Site admin
    Answer ✓

    "-" should not be displayed.

    You need to remove it from the data given to the table hen. Sorting can't filter the table. You could apply a custom filter, but it would be better to just not draw rows you don't want.

    Having said that, it appears to sort correctly in the example above.

    Allan

This discussion has been closed.