Jump to content

Recommended Posts

Posted

I have a problem with TUnimDBListGrid.

I want to group records by date, but the component sorts the groups as strings.
For example, "01-04-2026" is treated as later than "31-01-1998".

I do not want to change the displayed format, because "2026-04-01" is technically sortable, but it does not look right for users.

Is there any way to keep the display format as dd-mm-yyyy and still sort/group the dates correctly?

I also tried using values like:

<a style="display:none">20260401</a>01-04-2026

but the group header does not seem to interpret it as HTML.

 image.thumb.png.eb20e779f403130f67767df6bb178ddb.png

Posted
19 hours ago, PS1 said:

but i changed it so it shows the problem.

Can you try this approach?

function afterCreate(sender) 
{
    var fieldIndex = "0"; // set your field index here

    function parseDateValue(v) {

        if (!v) return v;

        if (v instanceof Date) return v.getTime();
        if (typeof v === "number") return v;

        if (typeof v === "string") {

            if (v.length >= 10 && v[2] === '.' && v[5] === '.') {
                var d = Ext.Date.parse(v, "d.m.Y H:i:s") || Ext.Date.parse(v, "d.m.Y");
                if (d) return d.getTime();
            }

            if (v.length >= 10 && v[2] === '-' && v[5] === '-') {
                var d = Ext.Date.parse(v, "d-m-Y H:i:s") || Ext.Date.parse(v, "d-m-Y");
                if (d) return d.getTime();
            }

            if (v.length >= 10 && v[4] === '-' && v[7] === '-') {
                var d = Ext.Date.parse(v, "Y-m-d H:i:s") || Ext.Date.parse(v, "Y-m-d");
                if (d) return d.getTime();
            }

            if (v.indexOf('T') > -1) {
                var d = new Date(v);
                if (!isNaN(d)) return d.getTime();
            }
        }

        return v;
    }

    var store = sender.getStore();
    if (!store) return;

    var dir = 'DESC';

    store.setGrouper({
        direction: dir,
        groupFn: function(record) {
            return parseDateValue(record.get(fieldIndex));
        }
    });

    store.setSorters([{
        direction: dir,
        sorterFn: function(a, b) {
            var v1 = parseDateValue(a.get(fieldIndex));
            var v2 = parseDateValue(b.get(fieldIndex));

            if (v1 > v2) return 1;
            if (v1 < v2) return -1;
            return 0;
        }
    }]);

    if (sender.groupingInfo && sender.groupingInfo.header) {
        sender.groupingInfo.header.config.tpl.html = '{name:this.formatValue}';

        sender.groupingInfo.header.config.tpl.formatValue = function(v) {
            if (!isNaN(v)) {
                var d = new Date(parseInt(v, 10));
                if (!isNaN(d))
                    return Ext.Date.format(d, 'd-m-Y');
            }
            return v;
        };
    }
}

 

  • Thanks 1
×
×
  • Create New...