ypnos-web / cakephp-datatables

CakePHP3 Plugin for DataTables plug-in for jQuery
MIT License
27 stars 24 forks source link

dt.render.date should check for a null value #66

Open challgren opened 6 years ago

challgren commented 6 years ago

When using the provided dt.render.date if the date passed is null an error will occur.

ypnos-web commented 6 years ago

This is in line with the default renderer. What would you want it to do/render in case of missing date? Do you think the behavior should be configurable (e.g. a 'fallback text' argument)?

I am also open for a pull request in this matter.

challgren commented 6 years ago

Well so here's one thing I found out. If you are storing the data in the DATE format it is returned as a DATE relative to UTC even if you override the server timezone and the database timezone. I do think the fallback text would be a good option. Below is a sample of what my custom render is.

$.fn.dataTable.render.date = function (fallback)
{
    return function (data, type, full, meta)
    {
        if (type === 'display') {
            if (data) {
                var date = new Date(data.toString().substring(0, 4), (parseInt(data.toString().substring(5, 7)) - 1), data.toString().substring(8, 10));
                return date.toLocaleDateString(document.documentElement.lang);
            } else if (fallback !== undefined) {
                return fallback;
            } else {
                return data;
            }
        }
        return data;
    }
};
$.fn.dataTable.render.date_time = function (fallback) {
    return function (data, type, full, meta)
    {
        if (type === 'display') {
            if (data) {
                var date = new Date(data);
                return date.toLocaleDateString(document.documentElement.lang) + ' ' + date.toLocaleTimeString(document.documentElement.lang);
            } else if (fallback !== undefined) {
                return fallback;
            } else {
                return data;
            }
        }
        return data;
    }
};
ypnos-web commented 6 years ago

Yes, your observation is correct. What I don't understand is why you use custom parsing of the string in the first method.

Would you like to prepare a pull request with the fallback mechanics?