用“OR"过滤和“AND"多个字段的条件 [英] Filter with "OR" And "AND" Conditions on Multiple Fields
问题描述
我想实现以下 where 条件.如何在 UI5 中创建过滤器?
field-A NE 'O' and ( field-B 包含 'search-text' 或 field-C 包含 '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/.可以在此处找到完整列表.>
在实例化过滤器时,使用属性 filters
和 filters
代替 path
、operator
和 value1
and
组合多个过滤器,如Filter
API 参考.
在我们的例子中,我们定义了三个过滤器:
- 第一个
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) {返回新过滤器({过滤器: [new Filter("Field-B", FilterOperator.Contains, query),new Filter("Field-C", FilterOperator.Contains, query),],和:假的,});},
最后,当用户输入搜索查询时,我们将这两个过滤器与应用在 ODataListBinding.
onSearch: function(event) {this.byId("myList").getBinding("items").filter(new Filter({过滤器: [this.getInitialFilter(),this.getSearchFilters(event.getParameter("query")),],和:真的,}), FilterType.Application);},
注意:过滤时,请记住应用FilterType "Application"
作为 myListBinding.filter
中的第二个参数,让框架知道过滤器是由您设置的(应用程序)而不是通过控件.否则,列表绑定会将您的过滤器与最初设置的应用程序过滤器结合起来.
I want to implement the below where condition. How do I create the filter in UI5?
field-A NE 'O' and ( field-B contains 'search-text' or field-C contains 'search-text' )
The backend business scenario:
- Apply the filter
field-A NE 'O'
when binding list. - Apply the filter
( field-B contains 'search-text' or field-C contains 'search-text' )
to implement the search function on the search field.
Filter instances:
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);
Here is a minimal example of combining multiple filters using OData from Northwind: https://embed.plnkr.co/AoIZI4/. The complete list can be found here.
When instantiating a filter, instead of path
, operator
, and value1
, use the properties filters
and and
to combine multiple filters as shown in the Filter
API reference.
In our case, we define three filters:
- One for the first
field-A NE 'O'
which is also used on the initial binding in the Plunker example above (Filter 1) - And for the other two in the search event handler with
and: false
meaningOR
(Filter 2).
Filter 1:
getInitialFilter: function() {
return new Filter("Field-A", FilterOperator.NE, "O");
},
Filter 2:
getSearchFilters: function(query) {
return new Filter({
filters: [
new Filter("Field-B", FilterOperator.Contains, query),
new Filter("Field-C", FilterOperator.Contains, query),
],
and: false,
});
},
Finally, when the user enters a search query, we combine those two filters with and: true
applying on the ODataListBinding.
onSearch: function(event) {
this.byId("myList").getBinding("items").filter(new Filter({
filters: [
this.getInitialFilter(),
this.getSearchFilters(event.getParameter("query")),
],
and: true,
}), FilterType.Application);
},
Note: When filtering, keep in mind to apply the FilterType "Application"
as the 2nd argument in myListBinding.filter
to let the framework know that the filter was set by you (application) and not by a control. Otherwise, the list binding will combine your filters with the application filters which were initially set.
这篇关于用“OR"过滤和“AND"多个字段的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!