Skip to Content
0

Get first item of select box when page first loads

May 02 at 04:11 PM

49

avatar image

Hi,

I have summary screen that must load accounts in a select box and populate all the fields in the form using oData and basing it on an ID in the URL.

But I need to load the select box in order to get the right account to load the rest of the page and the first account has to already be filled once page loads (so without anyone selecting in the the select box). I am only able to load the combo box after rendering and not onInit and I am not able to get the items of the select box in order to get the account number.

I am able to load the entire page if it is only ONE account and I am able to load all the account numbers in the select box but when the page is loaded then it is not based on the account number.

What is a better way to load the select box and be able to get the items in order to load the rest of the page?

Would getting the oData and turning into a json onInit be better? if so, can I get an example, I've tried some examples and have been unsuccessful.

onInit: function() {
//get URL and get ID from it
        var url = window.location.href;
        var id = url.substring(urlString.length - 10, urlString.length);
       $.sap.urlId = id;
//NOT WORKING: SAP Odata service url
    var sServiceUrl = "/SummaryScreenSet('" + id + "')";
    //Create OData model
    var oModel = new sap.ui.model.odata.v2.ODataModel(sServiceUrl, {
	useBatch: true});
    var oODataJSONModel = new sap.ui.model.json.JSONModel();
    var oData = oModel.read("/SummaryScreenSet", {
	method: "GET",
	success: function(oData2, oResponse) {
	//Create JSON Model
	sap.m.MessageBox.show("Success!");
	oODataJSONModel.setData({modelData: oData2.results});},
	error: function(oError) {
	sap.m.MessageBox.show("Error!");}});
},

onAfterRendering: function() {
	var id = $.sap.urlId;
        var comboB = this.getView().byId("accountBox");
	comboB.bindAggregation("items", "/SummaryScreenSet", new sap.ui.core.ListItem({text: "{ACCOUNT}"}));
	var oFilterDist = new sap.ui.model.Filter("PARTNER", sap.ui.model.FilterOperator.EQ, id);
	comboB.getBinding("items").filter(oFilterDist);
},
 
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

VIPLOVE KHUSHALANI May 03 at 06:15 AM
0

Hi ..

Few things to clarify .

1. Why are you getting the id from the URL .

2. If you are populating combobox on rendering than after the combobox gets populated or bind than call the rest of the page data ..as you say it depends on the value of combobox data .. than why r you calling the service on Init

Thanks

Viplove

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Viplove,

So this Fiori app is being used inside another application where the ID is in the URL, I need that in order to get the accounts that will be populated in the select box. I would like to be able to populate the select box with the accounts and already have the rest of the screen populated with the first account number but I have been having a hard time figuring it out.

I had to populate the combo box on rendering because when I tried to do it on init it would say the object is undefined.

I would like to just call the backend only on init and populate everything but I can't seem to figure out how to get the data into a JSON model and then bind it to the controls. The only way I was able to do it was by getting the data and binding to specific controls.

Thanks!

summaryscreen.png (22.9 kB)
0

Ok ..

First of all .. making your app dependent to the url is not good .. you need to pass the value by some other means .. if its routing than pass it as routing parameter while calling the route function..

Second once you passed the Id than on getting the Id call the function which gets other data .. or even you can the select box data service and bind it and make the selected key of selectbox to the passed Id on change event of binding.

Thanks

0
tisha dharod May 04 at 08:39 AM
0

Hi

Please bind the property "selectedKey" of combo box with path to view the default first value of the array. Here as an example I have used Countrycollection array and binded the first row of the array

<ComboBox items="{ path: '/CountriesCollection', sorter: { path: 'text' } }"

selectedKey="{/CountriesCollection/0/key}">

<core:Item key="{key}" text="{text}" />

</ComboBox>

Share
10 |10000 characters needed characters left characters exceeded