简体   繁体   English

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

[英]SAPUI5 Filter OData model based on fields using formatter

I have a List that contains ObjectListItems with content provided by an OData service. 我有一个包含ObjectListItems的列表,其内容由OData服务提供。 One of this contents is the title and the property has the value as follows: 标题之一是该内容,该属性的值如下:

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

As you can see there is a formatter in this title. 如您所见,此标题中有一个格式化程序。 The OData will bring a value like "available" or "disabled" and the formatter will transform it on the text for the specific language of the user. OData将带来一个诸如“ available”或“ disabled”的值,并且格式化程序会将其转换为针对用户特定语言的文本。

I'm implementing a search capability on this List and it works fine, the problem is that it searchs only on the "available" and "disabled" values, and not in the formatted texts as it would be expected as this are not the values recognized by the user. 我正在此列表上实现搜索功能,并且工作正常,问题是它仅在“可用”和“禁用”值上搜索,而不是在格式化文本中搜索,因为这不是值被用户认可。

The filter code is: 过滤器代码为:

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);
},

Any idea on how to consider the formatter on the filter and not only the raw data? 关于如何考虑过滤器上的格式化程序而不是原始数据的任何想法?

Solution Considering you are doing only client side search: Assumption: if you have grouping in the list.. 解决方案考虑到您仅在进行客户端搜索:假设:如果列表中有分组。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM