2017-04-25 23 views
3

嗨,我与它背后的代码searchfield:添加第二过滤器不除去存在的

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

和过滤器的按钮,应该添加aditional的过滤器。像这样:

onSetFilter : function(oEvent) { 
    var andFilter = []; 
    andFilter.push(new Filter("BusArea", FilterOperator.EQ, "5000")); 
    this._applySearch(andFilter); 
}, 

但是,当然,“BusArea”部分应该依赖于选择哪些过滤器。它可能超过1个过滤器。该_applySearch功能如下:

_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")); 
    } 
} 

的问题是,当我通过过滤器按钮添加一个过滤器,从搜索栏过滤器消失,约于其他方式。我如何更改我的代码,以便我可以添加过滤器而不删除现有的过滤器?

+0

时 输入模拟SearchField

两者都对全球“filtersModel”绑定,都调用_calculateFilters()函数中,我们通过保持所施加多个过滤器控制器级别的过滤器数组。因此,我们首先将过滤器推送到数组,例如this.aFilters.push(oFilter),然后将this.aFilters应用于绑定。寻找更好的解决方案。 –

回答

1

一个解决方案是从绑定信息获取过滤器,并使用and与新过滤器一起推回。

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

好的,我喜欢这样,但这种方式的过滤器将堆叠。你对如何解决这个问题有任何想法吗? – freshrebel

+0

@freshrebel我现在明白你的观点。我可能会误解你想达到的目标。每次搜索按钮被击中时,是否有可能循环遍历所有的Filters控件,并重新构建具有值的筛选器? – Allen

+0

好的,但我的过滤器(所以不是搜索)来自这样的对话框:https://sapui5.hana.ondemand.com/explored.html#/sample/sap.m.sample.ViewSettingsDialog/preview。即使在关闭对话框后,我可以从对话框中获取值吗? – freshrebel

1

在聊天后,我已经使用全局模型做了这个片段。

https://jsbin.com/pewavuhonu/edit?html,output

组合框和按钮模拟你的对话框。 submiting的信息

<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t \t <meta http-equiv='X-UA-Compatible' content='IE=edge'> 
 
\t \t <meta charset="utf-8"> 
 

 
\t \t <title>MVC with XmlView</title> 
 

 
\t \t <!-- Load UI5, select "blue crystal" theme and the "sap.m" control library --> 
 
\t \t <script id='sap-ui-bootstrap' 
 
\t \t \t src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js' 
 
\t \t \t data-sap-ui-theme='sap_bluecrystal' 
 
\t \t \t data-sap-ui-libs='sap.m' 
 
\t \t \t data-sap-ui-xx-bindingSyntax='complex'></script> 
 

 

 
\t \t <!-- DEFINE RE-USE COMPONENTS - NORMALLY DONE IN SEPARATE FILES --> 
 

 
\t \t <!-- define a new (simple) View type as an XmlView 
 
\t \t - using data binding for the Button text 
 
\t \t - binding a controller method to the Button's "press" event 
 
\t \t - also mixing in some plain HTML 
 
\t \t note: typically this would be a standalone file --> 
 

 
\t \t <script id="view1" type="sapui5/xmlview"> 
 
\t \t <mvc:View xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" controllerName="my.own.controller"> 
 
\t \t \t <Panel headerText="Filters"> 
 
\t \t \t \t <VBox> 
 
\t \t \t \t \t <HBox> 
 
\t \t \t \t \t \t <Label text="Filter by Customer:" class="sapUiSmallMarginTop sapUiSmallMarginEnd"/> 
 
\t \t \t \t \t \t <ComboBox id="comboBox" selectedKey="{filtersModel>/customerFilter}"> 
 
\t \t \t \t \t \t \t <items> 
 
\t \t \t \t \t \t \t \t <core:Item key="VINET" text="VINET" /> 
 
\t \t \t \t \t \t \t \t <core:Item key="TOMSP" text="TOMSP" /> 
 
\t \t \t \t \t \t \t \t <core:Item key="HANAR" text="HANAR" /> 
 
\t \t \t \t \t \t \t \t <core:Item key="VICTE" text="VICTE" /> 
 
\t \t \t \t \t \t \t \t <core:Item key="SUPRD" text="SUPRD" /> 
 
\t \t \t \t \t \t \t </items> 
 
\t \t \t \t \t \t </ComboBox> 
 
\t \t \t \t \t \t <Button text="Apply this Filter" press="_calculateFilters"></Button> 
 
\t \t \t \t \t </HBox> 
 
\t \t \t \t </VBox> 
 
\t \t \t \t <VBox> 
 
\t \t \t \t \t <HBox> 
 
\t \t \t \t \t \t <Input value="{filtersModel>/shipAddressFilter}" id="input" submit="_calculateFilters" width="500px" placeholder="Filter by ShipAddress: Write and enter for filtering"/> 
 
\t \t \t \t \t </HBox> 
 
\t \t \t \t </VBox> 
 
\t \t \t </Panel> 
 
\t \t \t <Panel> 
 
\t \t \t \t <List id="list" items="{/Orders}"> 
 
\t \t \t \t \t <StandardListItem title="{CustomerID}" info="{ShipAddress}"/> 
 
\t \t \t \t </List> 
 
\t \t \t </Panel> 
 
\t \t </mvc:View> 
 
     </script> 
 

 

 
\t \t <script> 
 
\t \t \t // define a new (simple) Controller type 
 
\t \t \t sap.ui.controller("my.own.controller", { 
 
\t \t \t \t 
 
\t \t \t \t onInit: function(){ 
 
\t \t \t \t \t var oFiltersModel = new sap.ui.model.json.JSONModel(); 
 
\t \t \t \t \t sap.ui.getCore().setModel(oFiltersModel, "filtersModel"); 
 
\t \t \t \t }, 
 
\t \t \t \t 
 
\t \t \t \t _calculateFilters: function(){ \t \t \t \t \t 
 
\t \t \t \t \t var \t oSelect = this.getView().byId("comboBox"), 
 
\t \t \t \t \t \t oListBinding = this.getView().byId("list").getBinding("items"), 
 
\t \t \t \t \t \t oFiltersModel = sap.ui.getCore().getModel("filtersModel"), 
 
\t \t \t \t \t \t oCustomerFilterValue = oFiltersModel.getProperty("/customerFilter"), 
 
\t \t \t \t \t \t oShipAddressValue = oFiltersModel.getProperty("/shipAddressFilter"), 
 
\t \t \t \t \t \t oFilters = []; 
 
\t \t \t \t \t 
 
\t \t \t \t \t if(oCustomerFilterValue){ 
 
\t \t \t \t \t \t oFilters.push(new sap.ui.model.Filter("CustomerID", "EQ", oCustomerFilterValue)); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t if(oShipAddressValue){ 
 
\t \t \t \t \t \t oFilters.push(new sap.ui.model.Filter("ShipAddress", "Contains", oShipAddressValue)); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t 
 
\t \t \t \t \t oListBinding.filter(oFilters); 
 
\t \t \t \t } 
 
\t \t \t }); 
 
\t 
 
\t 
 
\t 
 
\t \t \t /*** THIS IS THE "APPLICATION" CODE ***/ 
 

 
\t \t \t // create some dummy JSON data 
 
\t \t \t var data = { 
 
\t \t \t \t actionName: "Say Hello" 
 
\t \t \t }; 
 

 
\t \t \t // instantiate the View 
 
\t \t \t var myView = sap.ui.xmlview({viewContent:jQuery('#view1').html()}); // accessing the HTML inside the script tag above 
 

 
\t \t \t // create a Model and assign it to the View 
 
\t \t \t var uri = "https://cors-anywhere.herokuapp.com/services.odata.org/Northwind/Northwind.svc"; // local proxy for cross-domain access 
 
\t \t \t var oModel = new sap.ui.model.odata.ODataModel(uri, { 
 
\t \t \t \t maxDataServiceVersion: "2.0" 
 
\t \t \t }); 
 
\t \t \t myView.setModel(oModel); 
 
    \t  \t 
 

 
\t \t \t // put the View onto the screen 
 
\t \t \t myView.placeAt('content'); 
 

 
\t \t </script> 
 
\t 
 
\t </head> 
 
\t <body id='content' class='sapUiBody'> 
 
\t </body> 
 
</html>

+0

谢谢。但这样,每次更改过滤器或执行新搜索时,过滤器都会堆叠。关于如何解决这个问题的任何想法? – freshrebel

+0

为Filter对象赋予和标识,以便您可以在当前过滤器数组中标识它们。 然后每当你添加一个新的过滤器,检查是否有另一个与新的过滤器具有相同的ID。如果是这样,弹出它并推新的。 –

+0

在这个链接:https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.Filter.html我看不到任何参数给过滤器一个id。所以我该怎么做? – freshrebel