advice on a render for dates

advice on a render for dates

MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi,

I have dates in my table who can be 00/00/00 like in this example :

"dateDebut": {
            "display": "03/05/2021",
            "sort": "2021-05-03",
            "annee": 2021
        },
        "dateFin": {
            "display": "00/00/00",
            "sort": "0000-00-00"
        },
        "dateMaj": {
            "display": "04/07/2021",
            "sort": "2021-07-04"
        },

I've created the following function to use it in the columns defs of my datatable.
{ data: 'dateFin', render: render_control_date }

It works great but I'm wondering if I could have done it better (I'm still learning...)

function render_control_date( data, type, row ) {   
        if (type === 'display') {
                if ( data.display === '00/00/00' ) {
                    var value = '';
                } else {
                    var value = data.display;
                }
                return value;
         }
        if (type === 'sort') {
                return data.sort;
         }
    return data;
}

Thanks for your advices

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Looks really good to me. If you wanted to not require a sort parameter in the source data, then your rendering function could convert the formatted date into an ISO8601 date or a unixtime stamp, but that isn't needed if you have that sort parameter.

    Only one thing I would suggest, which is to have the ISO8061 value returned for type === 'type' as well as 'sort'. That will let DataTables see it as a "date" type.

    Allan

  • MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

    Thank you Allan.

    I need the sort parameter.

    Sorting works with YYYY-MM-DD format.
    If I understand you well, this format is like text for datatable ? What kind of problem could happen ? Maybe if I want to do things like counting days for example or compare dates it will be complicated ?

    I will watch how I can change the data collection from the application to use ISO8061 format.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    DataTables will correctly detect ISO8601 formatted dates as dates, and treat them accordingly (sorting is the only real impact for DataTables core, but extensions like SearchBuilder can offer date specific search options).

    Generally what I would suggest is that you use ISO8601 as the format for the date "over the wire" (i.e. what gets downloaded from the server to the client) and then use a renderer such as DataTable.render.date() to display it in a locale specific format. There is an example of that here. That way the display works for however is looking at it - MM/DD/YY, DD/MM/YY or whatever.

    However, your approach is fine and will work perfectly well.

    Allan

  • MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2
    edited October 2023

    Hi Allan,

    Thanks again for your explanations.

    The application I'm using is based on 4D, I can choose to format the data so the dataset is ready to use most of the time.
    I like it that way, and I'm trying to use JS for everything else.
    For example, for the dates, I have a few choices :
    https://doc.4d.com/4Dv19/4D/19.6/String.301-6269759.en.html

    I will have a look at SearchBuilder extension, it will probably give me more ideas or things to try.

Sign In or Register to comment.