cancel
Showing results for 
Search instead for 
Did you mean: 

Defaulting view controls with parameters passed via URL

aabhas_wilmar
Contributor
0 Kudos

Hi All,

Let's say UI5 app runs via following URL (and in addition, I am passing a URL parameter param01):

http://mydomain.com:1080/sap/bc/ui5_ui5/zmyui5app/index.html?sap-client=030&param01=1000

Obviously, I can read this parameter using following statement in view controller onInit() or Component.js:

var sParam01 = jQuery.sap.getUriParameters().get("param01"); 


//Now sParam01 is set to 1000 from URL parameter.



Let's say we have a Select (dropdown combo box) on the view.


Can we set the default value (setSelectedItem) of this Select using sParam01?


I have tried it in all functions responsible for the creation/load of view. I can access the value and the label and the value sets ok but it is not displayed. The reason for this is binding (Select is bound to JSON datamodel) occurs AFTER onInit() and overwrites what was assigned from the parameters. I have also tried to rerender the view where possible.


The requirement is such that I have an app which defaults to certain values. Each business unit would like to see their own defaults as soon as the app loads.


What can be done to achieve this? Can someone show an example?


Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

aabhas_wilmar
Contributor
0 Kudos

Anyone?

dibyajyoti_nanda
Participant
0 Kudos

Hi Aabhas,

can you please explain again .

what i understand from your explanation that you have a dropdown in your application which is bound to some predefined json model with predefined data .

and you want to set one new value extracted from request parameter in runtime .

is this the requirement ?

[v1,v2,v3] (predefined json model) and for example now the dropdown is default selected to v1 but instead of this you want to bind to param1

please correct me if i am wrong

regards

Dibyajyoti Nanda

aabhas_wilmar
Contributor
0 Kudos

Hi Dibyajyoti,

You got it right except for the last part.

If the dropdown is bound to JSON [v1, v2, v3] and defaulted to v1, I want to be able to default it to either v2 or v3 depending on the parameter.

The parameter will always be one of the JSON values to which drop down is already bound to.

Hope it's more clear now.

regards,

Aabhas

dibyajyoti_nanda
Participant
0 Kudos

Hi Aabhas,

please follow below steps .

http://localhost:57940/SapUI5TestApp/index.html?param01=AI

S1.view.xml

<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"

  xmlns="sap.m" controllerName="sapui5testapp.S1" xmlns:html="http://www.w3.org/1999/xhtml">

  <Page title="Title">

  <content>

  <Select id="s1" items="{/CountryCollection}" change="onchange">

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

        </Select>

  </content>

  </Page>

</core:View>

S1.controller.js

onInit: function() {

   var data = {

  "CountryCollection": [

  { "CountryId": "AD", "CountryName": "Andorra" },

  { "CountryId": "AE", "CountryName": "United Arab Emirates" },

  { "CountryId": "AF", "CountryName": "Afghanistan" },

  { "CountryId": "AG", "CountryName": "Antigua and Barbuda" }, 

  { "CountryId": "AI", "CountryName": "Anguilla" },

  { "CountryId": "AL", "CountryName": "Albania" },

  { "CountryId": "AM", "CountryName": "Armenia" },

  { "CountryId": "AO", "CountryName": "Angola" }

  ],

  "Nationality": "AM"

  };

  this.getView().setModel(new sap.ui.model.json.JSONModel(data));

  var key = jQuery.sap.getUriParameters().get("param01");

  this.getView().byId("s1").setSelectedKey(key)

  },

hope this helps .

regards

Dibyajyoti Nanda

aabhas_wilmar
Contributor
0 Kudos

Hi Dibyajyoti,

Thanks, that worked in principle. The mistake I was making was also having the property "selectedKey" set in the view for the Select. This property was bound as well: selectedKey="{appData>/DefaultOrg}".

When I removed it, it started working for obvious reasons.

Thanks,

Aabhas

dibyajyoti_nanda
Participant
0 Kudos

Hi Aabhas,

Thanks for conformation .

I think I have answered correctly ?

regards

Dibyajyoti Nanda

Answers (0)