FIxed values on top of a column with formatted dates

FIxed values on top of a column with formatted dates

luca.gluca.g Posts: 12Questions: 3Answers: 0
edited October 2020 in Free community support

Hello,

I have a column with dates formatted as, e.g., 30 Oct 2020 with:

$.fn.dataTable.moment( 'DD MMM YYYY' );

The problem is that some of the cells are empty. I'd like the empty cells to be shown at the top, and then the rest of the data in ascending order.

Adding

  var deadlineType = $.fn.dataTable.absoluteOrder( [
    { value: '', position: 'top' }
  ] );

[...]
    "columnDefs": [ 
    { "targets": 2, type: deadlineType } ]

to my code puts the empty cells on top; nevertheless the sorting is messed up, I suspect because the sorting is made for text. Nevertheless, $.fn.dataTable.absoluteOrderNumber doesn't work either. Is there and absoluteOrder plugin for dates formatted as mine?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,318Questions: 26Answers: 4,772

    Take a look at this thread that I just answered. See if the proposed solution will work for you.

    Kevin

  • luca.gluca.g Posts: 12Questions: 3Answers: 0

    Thank you very much. My data is provided server-side, not via ajax call. Not sure if this is why my code isn't working properly. I'm using this:

    "order": [[ 2, "asc" ]],
        "columns": [{
        data: 2,
        render: function (data, type) {
            var date = moment(data, [
                "DD MMM YYYY"
            ]);
            if (date != "") {
                return date;
            } else {
                if (type === 'sort') {
                  return ''
    
                }
    
                return "Invalid date";
             }
        },
    }]
    

    The column with the dates is column 2. Nevertheless I get the dates in my first column (where other data should be), and they're also not formatted as I want (e.g. 'Thu Dec 31 2020 00:00:00 GMT+0000'). What am I overlooking?

  • kthorngrenkthorngren Posts: 20,318Questions: 26Answers: 4,772
    Answer ✓

    Here is an example:
    http://live.datatables.net/caqozaze/1/edit

    It shows that the absolute order you tried isn't working because Datatables type detection sets the column type as string. For none date data we set a fake date value then use it with the absolute plugin to sort by that fake value. The last date column is just normal just to show the $.fn.dataTable.moment( 'DD MMM YYYY' ); format is working.

    Kevin

  • kthorngrenkthorngren Posts: 20,318Questions: 26Answers: 4,772
    Answer ✓

    Nevertheless I get the dates in my first column (where other data should be), and they're also not formatted as I want (e.g. 'Thu Dec 31 2020 00:00:00 GMT+0000'). What am I overlooking?

    You can use the datetime render to change the format.

    Kevin

  • luca.gluca.g Posts: 12Questions: 3Answers: 0

    Yes, thanks. The main issue though is that it's in the wrong column!

  • kthorngrenkthorngren Posts: 20,318Questions: 26Answers: 4,772
    Answer ✓

    The main issue though is that it's in the wrong column!

    Hmm. Not sure why the data would be in the wrong column. Can you post a link to your page or a test case replicating the issue so we can take a look?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • luca.gluca.g Posts: 12Questions: 3Answers: 0

    Ah, I didn't see your live example. That's now working, thank you so much!

This discussion has been closed.