How to use a table column filter with formatted co

2019-03-05 23:54发布

问题:

I have a JSON model which contains, among others, a few date values which are stored as epoch values:

var oData = [{
    string : "SomeValue",
    date   : 1404172800000
}];

When I load my model, I convert this epoch to a proper Javascript Date object using:

for (var i = 0; i < oData .length; i++) {
    var dateLong = oData[i].date;
    oData[i].date = new Date(dateLong);
}

In my table, I then render the column using a formatter function:

var oDateColumn = new sap.ui.table.Column({
    label: new sap.ui.commons.Label({
        text: "A Date"
    }),
    template: (new sap.ui.commons.TextView({
        text : {
            parts : [date],
            formatter : function(oValue) {
                if (oValue != undefined) {
                    var yyyy = oValue.getFullYear().toString();                                    
                    var mm   = (oValue.getMonth()+1).toString(); // getMonth() is zero-based         
                    var dd   = oValue.getDate().toString();             
                    return yyyy + '/' + (mm[1]?mm:"0"+mm[0]) + '/' + (dd[1]?dd:"0"+dd[0]);
                } else return "";
            }
        },
        textAlign  : sap.ui.core.TextAlign.Right
    })),
    sortProperty   : "date",
    filterProperty : "date",
    filterOperator : sap.ui.model.FilterOperator.EQ
});

This works ok, and the former epoch is now a date which is nicely rendered as '2014/07/01'

However, the filtering is not on the formatted date but on the original Date object -- if I filter on '2014/07/01' I get no results; if I filter on '1404172800000' I get the filtered results...

I tried using a formatter on the filterProperty but I wasn't able to get this to work.

Does anyone know how I can have users filter on the formatted date?

回答1:

Using a date type might solve this issue

var dateType = new sap.ui.model.type.Date({
 pattern: "yyyy/MM/dd"
});

...

sortProperty   : "date",
filterProperty : "date",
filterType: dateType

look at this example for a simple use case on birthday column



标签: sapui5