Skip to Content
0
Sep 27, 2019 at 01:50 PM

SAP UI5 - promise for view element (SmartField) has odata content?

423 Views Last edit Oct 01, 2019 at 03:42 PM 3 rev

I feel like I'm beating my head against a wall. I have a very simple requirement, which seems way too complex in UI5. I have created my own custom Object Page for a home-grown Customer entity and I want to filter the value list (of type fixed-values) of one SmartField using the selected value in a separate SmartField on that Object Page; for example, I want to filter states/provinces for a selected Country. I have added a method onCountryChange to the change event of the Country SmartField, and that works - when I select a Country value from that value list, the state/province list is filtered for that country; however, if I later go back into my Object Page in display or change mode, the change event does not get triggered, so the state/province value list does not get filtered. I tried calling onCountryChange directly in the onInit method of the Object Page, but that gives me an error because the "content" aggregation of the State/Province SmartField is still null, but I need the content aggregation in order to apply a filter to the value list - arggghhh! So, I tried to leverage the entitySetFound event of the State/Province SmartField - still no luck. I also tried using the innerControlsCreated event of the State/Province SmartField - nope.

So, my question ultimately is: has anyone found a simple way to accomplish filtering a SmartField value list of type fixed-values using the result of the previously/currently selected value of another SmartField? Or perhaps is there a promise/event that can be used once the content aggregation of the SmartField is not null?

methods onCountryChange and setStateFilter:

		onCountryChange: function () {
			var sCountryId, sStateId, sCityId;


			var oView = this.getView();
				sCountryId = sap.ui.core.Fragment.createId("idCustFrag", "idCountry");
				sStateId = sap.ui.core.Fragment.createId("idCustFrag", "idState");
			sCityId = sap.ui.core.Fragment.createId("idCustFrag", "idCity");
			
			var oState = oView.byId(sStateId);
			oState.attachEntitySetFound(this.setStateFilter("Customer"),this);
			
			// this.setStateFilter("Customer");
			
			var oCity = oView.byId(sCityId);
			oCity.attachEntitySetFound(this.setCityFilter("Customer"),this);
			// this.setCityFilter("Customer");
		},

		setStateFilter: function (sEntity) {
			var sCountryId, sStateId;


			var oView = this.getView();
			if (sEntity === "Customer") {
				sCountryId = sap.ui.core.Fragment.createId("idCustFrag", "idCountry");
				// Set country filter for state as well
				sStateId = sap.ui.core.Fragment.createId("idCustFrag", "idState");
			} else {
				// Customer Contact
				sCountryId = sap.ui.core.Fragment.createId("idContFrag", "idCountry");
				// Set country filter for state as well
				sStateId = sap.ui.core.Fragment.createId("idContFrag", "idState");
				// sCountryId = "idCountry";
				// sStateId = "idState";
			}
			var oStateContent = oView.byId(sStateId).getContent();

                        // ->>>> the next statement fails in onInit because  <<<<-
                        // ->>>> the content of the State SmartField is null <<<<-
			var oBinding = oStateContent.getBinding("items");
			var oFilter = new sap.ui.model.Filter({
				path: "CountryKey",
				operator: sap.ui.model.FilterOperator.EQ,
				value1: oView.byId(sCountryId).getValue()
			});
			oBinding.filter(oFilter);


			// var oFilter = new sap.ui.model.Filter({
			// 	path: "CountryKey",
			// 	operator: sap.ui.model.FilterOperator.EQ,
			// 	value1: oView.byId(sCountryId).getValue()
			// });
			// oBinding.filter(oFilter);
		},