用“OR"过滤和“AND"多个字段的条件 [英] Filter with "OR" And "AND" Conditions on Multiple Fields

查看:48
本文介绍了用“OR"过滤和“AND"多个字段的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下 where 条件.如何在 UI5 中创建过滤器?

field-A NE 'O' and ( field-B 包含 'search-text' 或 field-C 包含 'search-text' )

后端业务场景:

  1. 绑定列表时应用过滤器field-A NE 'O'.
  2. 应用过滤器( 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/.可以在此处找到完整列表.>

在实例化过滤器时,使用属性 filtersfilters 代替 pathoperatorvalue1and 组合多个过滤器,如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:

  1. Apply the filter field-A NE 'O' when binding list.
  2. 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 meaning OR (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆