absolute.js and date sorting

absolute.js and date sorting

silkspinsilkspin Posts: 141Questions: 32Answers: 5

I am using moment.js and datetime-moment.js with $.fn.dataTable.moment("DD/MM/YYYY"); declared in my js. All works fine and the dates sort correctly.

Then I installed absolute.js and I’ve tried both namesType and numbersType to sort the date. The blank spaces are moved to the bottom as expected, but the dates aren’t sorting in order. They are now sorting by the day only.

E.g.
01/10/2020
10/07/2020
16/08/2020
24/09/2020

So my question is, before creating a test, do these plugins work together and if they do is there something I can do to fix the sorting? If there is already a demo of the 2 scripts successfully working together then that would be great.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    Answer ✓

    The Datatablees type detection won't allow for multiple types to be assigned to a column. With the datetime-moment.js plugin the type detection will validate that all cells in the column are the same. Meaning it will make sure all the cells in the column match the format DD/MM/YYYY for your case. If not it will set the column type to string.

    Basically the two plugins will need to be combined into one to allow for the sorting the way you want.

    I don't know of any examples of the sorting the way you want.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    Thank you @kthorngren. I sort of guessed I might have to choose one plugin or the other after testing and not being able to find any examples myself. I appreciate your reply clarifying this. I'll just stick with datetime-moment.js for the ability to sort dates correctly which is more important to me.

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    edited October 2020

    I guess on thing you could try is to use orthogonal data and set a date for each of the non-date values, like 01/01/0001 for example.

    I haven't tried this so it may or may not work and may need changes to work but something like this:

        render: function ( data, type, row, meta ) {
          if (type === 'sort' || type === 'type') {
            if (data === 'my none date value') {
              return '01/01/0001';
            }
            return data;  // date value
          return data;
        }
    

    The idea is to modify the string to a date value that will sort the way you want. Also I expect it will affect the type detection to match your moment format.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    I tried to experiment with that but couldn't get it to work. I can see how it should work in this situation though because I've used orthogonal data before. I replaced if (data === 'my none date value') and tried both if (data === '') and if (data === null). Should there just be one return data;? I also got the error for the column "Requested unknown parameter".

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769
    Answer ✓

    I thought my code snippet looked funny. Its missing a } between lines 6 and 7, like this:

    render: function ( data, type, row, meta ) {
      if (type === 'sort' || type === 'type') {
        if (data === 'my none date value') {
          return '01/01/0001';
        }
        return data;  // date value
      }
      return data;
    }
    

    Here is a running example:
    http://live.datatables.net/tusaxitu/1/edit

    I also got the error for the column "Requested unknown parameter".

    The function always needs to return something.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    I saw that and added it, but then I removed the 2nd return data; :)

    Unfortunately this isn't going to work in my implementation because columns will be a mixture of dates and null values. I could add a space but it would probably be best to leave the sorting as it is.

    Thanks @kthorngren for helping. I've now learnt more about orthogonal data so it's still been beneficial.

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    implementation because columns will be a mixture of dates and null values.

    You can check for whatever data or type you want. I simply used a sting for the example.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    But when I tried both if (data === '') and if (data === null) that didn't work. Is this incorrect?

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    They both work here:
    http://live.datatables.net/tusaxitu/2/edit

    I guess it depends on what your data really is. Maybe its not a blank string ("") or a null but something else. PLease provide a link to your page or update the test case or create your own showing the data that you have so we can help.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    Hi Kevin. I think we are at cross purposes and moved away from the original problem. Your code does work fine for the sorting, but it was always keeping the spaces at the bottom which I was trying to achieve which has caused the confusion. I'll stick with the normal behaviour for datetime-moment.js and just use absolute.js on non-date format columns.

This discussion has been closed.