Sapui5 添加第二个过滤器而不删除现有过滤器

Sapui5 添加第二个过滤器而不删除现有过滤器,sapui5,Sapui5,嗨,我有一个搜索字段,后面有以下代码: onSearch : function (oEvent) { if (oEvent.getParameters().refreshButtonPressed) { // Search field's 'refresh' button has been pressed. // This is visible if you select any master list item. // In this c

嗨,我有一个搜索字段,后面有以下代码:

onSearch : function (oEvent) {
    if (oEvent.getParameters().refreshButtonPressed) {
        // Search field's 'refresh' button has been pressed.
        // This is visible if you select any master list item.
        // In this case no new search is triggered, we only
        // refresh the list binding.
        this.onRefresh();
    } else {
        var andFilter = [];
        var sQuery = oEvent.getParameter("query");
        if (sQuery && sQuery.length > 0) {
            // add filters
            var oTableSearchState = [];
            oTableSearchState = [new Filter("Supplier", FilterOperator.Contains, sQuery),                                        new Filter("BusArea", FilterOperator.Contains, sQuery),             new Filter("CostCenter", FilterOperator.Contains, sQuery),
            new Filter("FuncArea", FilterOperator.Contains, sQuery)];
            andFilter.push(new Filter(oTableSearchState, false));
        }
        this._applySearch(andFilter);
   }
},
和一个过滤器按钮,应该添加传统的过滤器。大概是这样的:

onSetFilter : function(oEvent) {
    var andFilter = [];
    andFilter.push(new Filter("BusArea", FilterOperator.EQ, "5000"));
    this._applySearch(andFilter);
},
_applySearch: function(andFilter) {
    var oViewModel = this.getModel("worklistView");
    this._oTable.getBinding("items").filter(andFilter, true);
    // changes the noDataText of the list in case there are no filter results
    if (andFilter.length !== 0) {
        oViewModel.setProperty("/tableNoDataText", 
        this.getResourceBundle().getText("worklistNoDataWithSearchText"));
    }
}
当然,“总线区域”部分应取决于选择的过滤器。它可能不止一个过滤器。_applySearch函数如下所示:

onSetFilter : function(oEvent) {
    var andFilter = [];
    andFilter.push(new Filter("BusArea", FilterOperator.EQ, "5000"));
    this._applySearch(andFilter);
},
_applySearch: function(andFilter) {
    var oViewModel = this.getModel("worklistView");
    this._oTable.getBinding("items").filter(andFilter, true);
    // changes the noDataText of the list in case there are no filter results
    if (andFilter.length !== 0) {
        oViewModel.setProperty("/tableNoDataText", 
        this.getResourceBundle().getText("worklistNoDataWithSearchText"));
    }
}

问题是,当我通过过滤器按钮添加过滤器时,搜索栏中的过滤器会消失,反之亦然。如何更改代码,以便在不删除现有过滤器的情况下添加过滤器?

一种解决方案是从绑定信息中获取过滤器,并使用
将其与新过滤器一起推回

this._oTable.getBindingInfo("items").filters.aFilters;

在聊天结束后,我用一个全局模型制作了这个片段

组合框和按钮模拟您的对话框。 输入模拟搜索字段

两者都绑定到全局“filtersModel”,并且在提交信息时都调用_calculateFilters()函数


带XmlView的MVC
//定义新的(简单的)控制器类型
sap.ui.controller(“my.own.controller”{
onInit:function(){
变量oFiltersModel=new sap.ui.model.json.JSONModel();
setModel(filtersModel的“filtersModel”);
},
_calculateFilters:函数(){
var oSelect=this.getView().byId(“组合框”),
oListBinding=this.getView().byId(“列表”).getBinding(“项目”),
oFiltersModel=sap.ui.getCore().getModel(“filtersModel”),
oCustomerFilterValue=oFiltersModel.getProperty(“/customerFilter”),
oShipAddressValue=oFiltersModel.getProperty(“/shipAddressFilter”),
of过滤器=[];
if(oCustomerFilterValue){
push(新的sap.ui.model.Filter(“CustomerID”、“EQ”、oCustomerFilterValue));
}
如果(oShipAddressValue){
push(新的sap.ui.model.Filter(“ShipAddress”、“Contains”、oShipAddressValue));
}
oListBinding.过滤器(过滤器的一部分);
}
});
/***这是“应用程序”代码***/
//创建一些伪JSON数据
风险值数据={
动作名称:“打招呼”
};
//实例化视图
var myView=sap.ui.xmlview({viewContent:jQuery('#view1').html()});//访问上面脚本标记内的HTML
//创建模型并将其指定给视图
var uri=”https://cors-anywhere.herokuapp.com/services.odata.org/Northwind/Northwind.svc"; // 用于跨域访问的本地代理
var oModel=new sap.ui.model.odata.ODataModel(uri{
maxDataServiceVersion:“2.0”
}); 
setModel(oModel);
//将视图放到屏幕上
myView.placeAt(“内容”);

我们通过在控制器级别维护过滤器阵列应用了多个过滤器。所以,我们首先将过滤器推送到数组中,比如,this.aFilters.push(oFilter),然后将this.aFilters应用到绑定中。寻找更好的解决方案。好的,我喜欢这样,但这样过滤器就会堆叠起来。你对如何解决那个问题有什么想法吗?我现在明白你的意思了。我可能误解了你想要实现的目标。是否有可能每次点击“搜索”按钮时,您都会循环“所有筛选器”控件,并再次使用值构建筛选器?好的,但我的筛选器(因此不是searchfiel)是从如下对话框中提交的:。即使对话框关闭,我也可以从中获取值吗?谢谢。但通过这种方式,每次更改过滤器或执行新搜索时,过滤器都会堆叠起来。有没有想过如何解决这个问题?给过滤器对象加上ID,这样你就可以在当前过滤器的数组中识别它们了。然后,无论何时添加新筛选器,都要检查是否有与新筛选器ID相同的筛选器。如果是的话,弹出它并推送你的新的。在这个链接上:我看不到任何参数可以给过滤器一个id。那么我怎么做呢?对不起@freshrebel你是对的。。。我没有意识到sap.ui.core.Filter直接继承自sap.ui.base.Object,而“id”属性是由sap.ui.base.ManagedObject引入的。。。在这种情况下,如果不再次从所有筛选控件收集所有筛选数据并每次重建整个筛选数组,我不知道如何才能做到这一点。也许一个好主意是在视图中创建一个“过滤器”模型,将您的过滤控件值与此模型绑定,并且每当您需要过滤表时,从模型中获取值并再次构建所有过滤器。好的,但是我的过滤器(所以不是searchfiel)是从如下对话框中提交的:。即使对话框关闭后,我也可以从中获取值吗?