Add a second filter without deleting the existing ones

Hi I have a search box with this code behind it:

onSearch : function (oEvent) {
    if (oEvent.getParameters().refreshButtonPressed) {
        // Search field '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);
   }
},

      

And a filter button that should add additional filters. Something like that:

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

      

But of course the "BusArea" part should depend on which filters are selected. There can be more than one filter. The _applySearch function looks like this:

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

      

the problem is that when I add a filter via the filter button, the filters from the search bar disappear and the other is arround. how can i modify my code so that i can add filters without removing existing ones?

+3


source to share


2 answers


One solution is to get filters from the binding information and re-connect to the new filter using and

.



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

      

+1


source


After our chat, I made this snippet using the global model.

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

The ComboBox and Button mimics your dialog. Login mimics SearchField



Both are bound to global "filtersModel" and both call the _calculateFilters () function when submitting information

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

		<title>MVC with XmlView</title>

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


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

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

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


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

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

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

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

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

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

Run codeHide result


+1


source







All Articles