我想实现下面的条件。如何在UI5中创建过滤器?
field-A NE 'O' and ( field-B contains 'search-text' or field-C contains 'search-text' )
后端业务场景:
field-A NE 'O'
。( field-B contains 'search-text' or field-C contains 'search-text' )
在搜索字段上实现搜索功能。过滤实例:
new sap.ui.model.Filter("field-A", sap.ui.model.FilterOperator.NE, "O");
new sap.ui.model.Filter("field-B", sap.ui.model.FilterOperator.contains, search-text);
new sap.ui.model.Filter("field-C", sap.ui.model.FilterOperator.contains, search-text);
以下是使用Northwind的OData组合多个过滤器的最小示例:https://embed.plnkr.co/AoIZI4/。完整列表可以找到here。
当实例化过滤器而不是path
,operator
和value1
时,使用属性filters
和and
组合多个过滤器,如API reference所示。
在我们的例子中,我们定义了三个过滤器
field-A NE 'O'
,它也用于上面的Plunker示例中的初始绑定(过滤器1)and: false
的搜索事件处理程序中的另外两个,意思是OR
(过滤器2)。过滤器1:
getInitialFilter: function() {
return new Filter("Field-A", FilterOperator.NE, "O");
}
过滤器2:
getSearchFilters: function(query) {
return new Filter({
filters: [
new Filter("Field-B", FilterOperator.Contains, query),
new Filter("Field-C", FilterOperator.Contains, query)
],
and: false
});
}
最后,当用户输入搜索查询时,我们将这两个过滤器与and: true
上的ODataListBinding相结合。
onSearch: function(event) {
this.getList().getBinding("items").filter(new Filter({
filters: [
this.getInitialFilter(),
this.getSearchFilters(event.getParameter("query"))
],
and: true
}));
}
var andFilter = [];
var orFilter = [];
orFilter.push(new sap.ui.model.Filter("title", sap.ui.model.FilterOperator.Equal, "filtervalue"));
orFilter.push(new sap.ui.model.Filter("status", sap.ui.model.FilterOperator.Equal, "filtervalue"));
andFilter.push(new sap.ui.model.Filter(orFilter, false));
orFilter = [];
orFilter.push(new sap.ui.model.Filter("title", sap.ui.model.FilterOperator.Equal, "filtervalue1"));
orFilter.push(new sap.ui.model.Filter("status", sap.ui.model.FilterOperator.Equal, "filtervalue1"));
andFilter.push(new sap.ui.model.Filter(orFilter, false));
oBinding.filter(new sap.ui.model.Filter(andFilter, true));
这应该转化为:
title=filtervalue || status=filtervalue && title=filtervalue1 || status=filtervalue1
使用以下语法对OR条件进行多重过滤:(对于AND,将false更改为true)
var InputFilter = new sap.ui.model.Filter({
filters: [
new sap.ui.model.Filter("Name", sap.ui.model.FilterOperator.EQ, inptval),
new sap.ui.model.Filter("ID", sap.ui.model.FilterOperator.EQ, inptval)
],
and: false
});
this.oModel.read("/Products", {
filters: [InputFilter],
success: jQuery.proxy(this._fnSuccessGet, this),
error: jQuery.proxy(this._fnErrorGet, this)
});