SAPUI5基于使用格式化程序的字段过滤OData模型

时间:2015-05-08 03:50:49

标签: javascript sapui5

我有一个包含ObjectListItems的List,其内容由OData服务提供。其中一个内容是标题,属性的值如下:

title="{ path: 'title', formatter: 'app.schedule.util.Formatter.titleText'}"

正如您所看到的,此标题中有一个格式化程序。 OData将带来像"可用的值#34;或者"禁用"格式化程序会将其转换为用户特定语言的文本。

我在此列表上实现搜索功能并且工作正常,问题是它只搜索"可用"并且"禁用"值,而不是格式化的文本,因为它不是用户识别的值。

过滤器代码为:

handleSearch : function (evt) {
    // create model filter
    var filters = [];
    var query = evt.getParameter("query");
    if (query && query.length > 0) {
        filters.push(new sap.ui.model.Filter("booked", sap.ui.model.FilterOperator.Contains, query));
        filters.push(new sap.ui.model.Filter("weekday", sap.ui.model.FilterOperator.Contains, query));
        filters.push(new sap.ui.model.Filter("title", sap.ui.model.FilterOperator.Contains, query));
        filters = new sap.ui.model.Filter(filters, false);
    }

    // update list binding
    var list = this.getView().byId("list");
    var binding = list.getBinding("items");
    binding.filter(filters);
},

如何考虑过滤器上的格式化程序,而不仅仅是原始数据?

1 个答案:

答案 0 :(得分:0)

解决方案考虑到您只进行客户端搜索: 假设:如果您在列表中进行分组..

handleSearch : function (evt) {
    sFilterPattern = evt.getParameter("query");
    sFilterPattern = sFilterPattern.toLowerCase();
        var aListItems = this.getView().byId("list").getItems();
        var bVisibility;
        var oGroupItem = null;
        var iCountInGroup = 0;
        for (var i = 0; i < aListItems.length; i++) {
            if (aListItems[i] instanceof sap.m.GroupHeaderListItem) {
                if (oGroupItem) {
                    if (iCountInGroup == 0) {
                        oGroupItem.setVisible(false);
                    } else {
                        oGroupItem.setVisible(true);
                        oGroupItem.setCount(iCountInGroup);
                    }
                }
                oGroupItem = aListItems[i];
                iCountInGroup = 0;
            } else {
                bVisibility = this.applySearchPatternToListItem(aListItems[i], sFilterPattern);
                aListItems[i].setVisible(bVisibility);
                if (bVisibility) {
                    iCountInGroup++;
                }
            }
        }
        if (oGroupItem) {
            if (iCountInGroup == 0) {
                oGroupItem.setVisible(false);
            } else {
                oGroupItem.setVisible(true);
                oGroupItem.setCount(iCountInGroup);
            }
        }
}

applySearchPatternToListItem:function(oItem, sFilterPattern) {
    if (sFilterPattern == "") {
        return true;
    }
    //uncomment to search in oModel data 
    /*var oIteshellata = oItem.getBindingContext(this.sModelName).getProperty();
    for (var sKey in oIteshellata) {
        var sValue = oIteshellata[sKey];
        // if (sValue instanceof Date) {
        // //just for the filter take each number as string
        // sValue = sValue.getDate() + "." +
        // sValue.getMonth() + "." + sValue.getFullYear();
        // }
        if (typeof sValue == "string") {
            if (sValue.toLowerCase().indexOf(sFilterPattern) != -1) {
                return true;
            }
        }
    }*/
    // if nothing found in unformatted data, check UI elements
    if ((oItem.getIntro() && oItem.getIntro().toLowerCase().indexOf(sFilterPattern) != -1) 
    || (oItem.getTitle() && oItem.getTitle().toLowerCase().indexOf(sFilterPattern) != -1) 
    || (oItem.getNumber() && oItem.getNumber().toLowerCase().indexOf(sFilterPattern) != -1) 
    || (oItem.getNumberUnit() && oItem.getNumberUnit().toLowerCase().indexOf(sFilterPattern) != -1) 
    || (oItem.getFirstStatus() && oItem.getFirstStatus().getText().toLowerCase().indexOf(sFilterPattern) != -1) 
    || (oItem.getSecondStatus() && oItem.getSecondStatus().getText().toLowerCase().indexOf(sFilterPattern) != -1)) {
        return true;
    }
    // last source is attribute array
    var aAttributes = oItem.getAttributes();
    for (var j = 0; j < aAttributes.length; j++) {
        if (aAttributes[j].getText().toLowerCase().indexOf(sFilterPattern) != -1) {
            return true;
        }
    }
    return false;
}