SAPUI5-如何从表中搜索格式化的日期

时间:2019-03-23 08:26:02

标签: sapui5

我有一个日期列,格式为MMM d,yyyy。如果用户搜索“ 2018”,该如何过滤?

XML

<table:Column filterProperty="StartDate" sortProperty="StartDate" width="100px" tooltip="{i18n>ttStartDate}">
    <Label text="Start Date"/>
    <table:template>
    <Text text="{path:'localModel>StartDate',formatter:'.formatter.date'}"/>
        </table:template>
</table:Column>

控制器

searchTable: function (evt) {
            var oTable = this.getView().byId("ordersTable");
            var searchTerm = evt.getParameter("query");
            if (searchTerm === "") {
                //When clear was pressed on search field
                oTable.getBinding("rows").filter(null);
            } else {
                var filters = [];
                var outerFilters = [];
                var searchTerms = searchTerm.split(" "); //words separated by space are considered as separate search terms. 
                for (var k = 0; k < searchTerms.length; k++) {

                    filters.push(new Filter("OrderType", sap.ui.model.FilterOperator.Contains, searchTerms[k]));
                    filters.push(new Filter("StartDate", sap.ui.model.FilterOperator.Contains, searchTerms[k]));
                    outerFilters.push(new Filter(filters));
                    filters = [];
                }
                oTable.getBinding("rows").filter(new Filter({
                    filters: outerFilters,
                    and: true //Default is OR between filters
                }));
            }
        },

接收日期为:开始日期:2019年3月23日星期六05:30:00 GMT + 0530
日期格式为: 2019年3月23日

我知道'Contains'仅适用于字符串,但如何使其适用于日期

1 个答案:

答案 0 :(得分:1)

我们遇到了类似的问题,有2种解决方案, 一个在客户端,另一个在服务器端。

客户端: 在该属性上使用格式化程序,将日期解析为字符串。

服务器端: 要求以字符串形式表示该日期的另一个属性。

查看您的代码,我可以看到您已经在使用格式化程序( formatter.date ),因此我们可以探索客户端选项。

在您的formater.date函数中,您可以执行以下操作:

date(yourDateField)
{
   let options = { year: 'numeric', month: 'short', day: 'numeric' };
   return yourDateField.toLocaleDateString("en-US", options);
}