cancel
Showing results for 
Search instead for 
Did you mean: 

SelectedKey not set in sap.m.Select SAPUI5/odata

Former Member
0 Kudos

Hi all,

I have problem with add a oData value in SelectedKey of a sap.m.Select.

I try serveral ways to add the value to SelectedKey but nothing works.

I have publish the project to Github

eu.centric.entrada.persData/WebContent/app/persData at master · rmiddelburg/eu.centric.entrada.persD...

problem is in the view personalDetailEdit.view.js in row 143/144

Kind Regards,

Richard

Accepted Solutions (0)

Answers (4)

Answers (4)

EdwarSoto
Participant
0 Kudos

Hi experts,

I have this and works for me,

<!--Binding label from Metadata. -->

<Label text="{/#Bankdetails/Bnksa/@sap:label}" />

<Select forceSelection="false"

id="Bankdetails-Bnksa"   <-- is very important set id for select. In other case do not works.!!!

  selectedkey = "{/toBankdetails/Bnksa}"  <-- Binding from oModel in View

  items="{   path : '/Sh_BankdetailsClassSet',   <-- Binding to get from service GW that contains list of values

filters: [

              {

  path:'Infty',

  operator :'EQ',

  value1 : '0009'

    }

]

}"

   >

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

  </Select>

MattHarding
Active Contributor
0 Kudos

Hi Richard,

Just ran into this problem, and figured a solution out and documenting here to make sure I understand it. Basically since the oData call is async, and the set key of the select happens prior; the first time you call this, it gets stuffed up, and if you've got 2-way binding, then it changes your value on you too to boot!

So in order to get this right; I suggest getting the value you want to set; then using

oModel.attachRequestCompleted("name of odata entityset", callbackfunctiontofixkey); in your onBeforeShow (or possibly onInit - I'm still playing around with it); then the callbackfunctiontofixkey can get the select control and set the selectedkey correctly (you may need to save this key in the case the select overwrites the key possibly).

Actually - Scratch that - what you should do is not bind the selectedKey in your view until the call back function is called then set it by getting the select control and binding the property selectedKey:

oSelect.bindProperty("selectedKey", "your odata entity property value");

Hope that helps you and others as it's not obvious that binding with odata in selects can have race conditions that cause the key to default and overwrite values.

Cheers,

Matt

Message was edited by: Matt Harding

Former Member
0 Kudos

Hi Jason,

I simplefied my view by defining the select into the view!

Like this:

      this.test = new sap.m.Select();

//     this.test.setModel(oModel1);

      this.nationalitySelectItems = new sap.ui.core.Item({key: "{Land1}",text: "{Land1}"});

      this.test.bindItems("/CountryCodesSet", this.nationalitySelectItems);

      this.test.setSelectedKey("NL");

    

      sap.m.MessageBox.show("chosen key" +this.test.getSelectedKey());

     this.test;

I see a list with countries and the first line is selected and has the value "TF". But Message shows that the selectedKey is "NL".

Richard

jmoors
Active Contributor
0 Kudos

Is Land1 your key, and does it contain NL in the list?

Former Member
0 Kudos

Hi Jason,

The list does contain "NL" but I have also tried other other key's like "DE" or "AF".

Richard

surendra_pamidi
Contributor
0 Kudos

Hi Middelburg,

Check this.. this.nationality.setSelectedKey('/Nationality'),

You are giving path but you are not binding the path value. I am thinking the above line you have check the syntax..

Do one thing get the values in an array and give that array values as below if there are 'n' number of values..

this.nationality.setSelectedKey(Nationality[0]), or if it is a single value give only 'Nationality'

Former Member
0 Kudos

Hello,

I tried your solution but nothing works.

Even when I use a hardcoded value like this it is not working.

source in onInit() in Controler of the view.

this.getView().addressTypeSelect.bindItems("/AddressSubtypeSet", this.getView().addressTypeSelectItems);

Source in the view

this.addressTypeSelect = new sap.m.Select("addressTypeSelectId",{selectedKey : "1"});


this.addressTypeSelectItems = new sap.ui.core.Item({

  key : "{Subtype}",

  text : "{SubtypeText}"

  });

  var addressTypeSimpleForm = new sap.ui.layout.form.SimpleForm({

       minWidth : 1024,

       maxContainerCols : 2,

       editable : true,

       content : [

                           new sap.ui.core.Title({text : "Adrestype"}),

                           new sap.m.Label({text : oBundle.getText("ADDRESSTYPE"),required : true}),

                            this.addressTypeSelect,

                        ]

  });

jmoors
Active Contributor
0 Kudos

Hi Richard,

I wonder if it could be a timing issue?? i.e. it looks like you are binding the items in your controller onInit method after the view has been created? perhaps the setSelectedKey is overridden when you bind the items??

Many thanks,

Jason

jmoors
Active Contributor
0 Kudos

Actually, Not sure if it's a timing issue as it looks like the onBeforeRendering, should handle the scenario, however might be worth investigating.

// note: setSelectedKey() method sometimes is called

// before the items are added, in this case the "selectedItem" association

// and "selectedItemId" property need to be updated in onBeforeRendering()

Regards,

Jason

Former Member
0 Kudos

Hoi Jason,

Thanks for helping me!

I think that is the case! I'am new at developing in SAPUI5 so I have figure out how to use and change data in SAPUI5 application. In Webdynpro I know exactly where how it works.

How can I update the selectionKey in onBeforeRedering() based on my sample. I'm a little bit lost.

Kind Regards,

Richard

jmoors
Active Contributor
0 Kudos

Hi Richard,

No worries, I think the onBeforeRendering method of the sap.m.Select control should automatically check the selectKey, so you shouldn't need to implement anything yourself.

I find it useful to look at the -dbg.js files for each of the controls to get a better understanding of how everything works, easier said than done when time is limited, but will be useful in the long run.

In terms of structuring the applications I think Bertram's Building Fiori like UIs, DJ's SublimeUI5 templates and Hans-Peter's SAPUI5 Boilerplate useful examples.

qmacro/SublimeUI5 · GitHub

Personally I prefer to implement the Views in XML, I've think it removes some of the complexity you can  inadvertently add to the views.

Going back to the select control it is possible to bind the selected item property to a model, so perhaps you can set on the model instead?

SAPUI5 SDK - Demo Kit

Regards,

Jason

Former Member
0 Kudos

Hi Jason,

I know flow of my application.  The first time I trigger the detail screen from my selectionscreen it uses the following flow.

Vier -> onInit -> onBeforeShow -> beforeRendering ->AfterRendering. Next time it only calls the onBeforeShow.

In the onBeforeShow I get my binding from the selection screen with following source

  onBeforeShow : function(oData) {

  this.getView().bindElement(oData.bindingContext.getPath());

  this.getView().nationality.setSelectedKey("NL");

   },

Also I set my selectedKey here.

Now I facing a problem that when I call the screen for the first time my selected key is not set.

When I leave the screen and open the screen again the selectedKey is set.

How Can I trigger the setSelectionKey so that it is set correctly the first time and how get I the value of a field "Country" which is available in the oData.bindingContext.getPath().

Kind regards,

Richard

jmoors
Active Contributor
0 Kudos

Hi Richard,

I'm not sure if you are over complicating this, I was able to bind to a simple example without the need to override any additional events??

http://jsbin.com/penab/1/edit

Regards,

Jason