cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve the Key value of a sap.m.RadioButtonGroup (xml view) / oData Model

GabrielGomezPad
Participant
0 Kudos

Hello Experts,

My challenge is to retrieve the Key value of a RBG in a Fiori app linked to an oData Service. From a Previous question I have obtained this answer: http://jsbin.com/hanadofuco/1/edit?js,output and it works fine in the json Model.

in my view.xml:


<RadioButtonGroup xmlns="sap.m"

id="Stoff"

Buttons="{/ZUI5_TEST_TVALUES_SET}"

width="500px"

class="sapUiMediumMarginBottom"

select="onSelectRadio">

<RadioButton text="{ATWTB}" key="{ATWRT}"/>

</RadioButtonGroup>

and the function in controller:


onSelectRadio: function(oEvent) {

var oSelectedIndex = oEvent.getParameter("selectedIndex");

var oModelo = this.getView().getModel();

var sKey  = oModelo.getProperty("/ZUI5_TEST_TVALUES_SET/"+oSelectedIndex+"/ATWRT");

  },

After getting the Model in "oModelo"  (image from Debugger below), "Skey" still results in "undefined".

How can I obtain in this case the ATWRT value? (Key value) or this sap.m.RadioButtonGroup?

Thank you

PD I was able to retrieve the text value of a RBG (thanks to this post->) But now the question is about key value.

Accepted Solutions (1)

Accepted Solutions (1)

former_member183518
Active Participant
0 Kudos

You can fetch the bindingContext from the instance of the selected item and get the data from the context.


onSelectRadio: function(oEvent) {

    var oSource = oEvent.getSource();

    var oContext = oSource.getSelectedButton().getBindingContext();

    var oData = oContext..getObject();

    console.log(oData.ATWRT);

}

GabrielGomezPad
Participant
0 Kudos

Thank you very much Sakthivel. Your solution works for obtaining the key value. I believe I understand now a little better the method "getBindingContext()" (I will take a look at the API at this point as you recommend).

@Karthik and @Dennis: your help is very appreciated I still learning from each of you.

Best Regards,

Gabriel

former_member183518
Active Participant
0 Kudos

No, it's just not only the key value ! The variable oData will contain all the properties of the selected item's entity. Just try logging the value of variable 'oData'.

Say, when you access oData.ATWTB --> Kunstleder. Here the property ATWTB isn't the key right ?

GabrielGomezPad
Participant
0 Kudos

Hello Saktivel,

The variable oData will contain all the properties of the selected Item you are right. The property ATWTB is not the key, but the name of the Item. The key value is ATWRT and that makes your solution correct.

Best Regards,

Gabriel

Answers (2)

Answers (2)

karthikarjun
Active Contributor
0 Kudos

Hi Gabriel,

Will this help?

JS Bin - Collaborative JavaScript Debugging

Thanks,

Karthik A

GabrielGomezPad
Participant
0 Kudos

Thank you Karthik for correcting the initial JsBin. The question still remains: I wanted to try the same for a Fiori App that consumes Odata. The first step was to get the model:

"var oModel = this.getView().getModel();" 

According to the debbuger, the model is obtained

The second step is to access the key of the radiobutton (I have not achieved that task). The Path is different from the one in the JsBin and I only obtain "undefined" Please see the first image I have included in the description.

I appreciate your help in advance,

Kind Regards,

Gabriel

karthikarjun
Active Contributor
0 Kudos

oModelo.getData().getProperty( Your Collection Name)

Or

oModelo.oData. ( Your Collection Name).ATWRT

Could you please try this..

Thanks,

KA

GabrielGomezPad
Participant
0 Kudos

Many Thanks Karthik for your answer. Perhaps I am not writing the collection name in the right way. In the screenshot of the debugger, I have highlighted in blue the path for each radiobutton.

I have tried writting the collection name:

- In this way -> "/ZUI5_TEST_TVALUES_SET/" ->

or rather this one? "/ZUI5_TEST_TVALUES_SET(ATNAM='FIORI_BEZUGSSTOFF',ATWRT='KL')"

I am obtaining from the debbuger the message: "Uncaught TypeError: Cannot read property 'getProperty' of null" in both cases.

I appreciate your support,

Kind Regards,

Gabriel

former_member182862
Active Contributor
0 Kudos
GabrielGomezPad
Participant
0 Kudos

Thank you very much Dennis for your reply.  I have tried the "getSelectedButton" Method following your solution.  I might be missing the best way to adapt it to my code. in the Detail.controller.js file I declare the function as follows:


onSelectRadio: function(oEvent) {

var btn = this.getSelectedButton();

var sKey  = btn.getBindingContext().getObject().key);

alert(sKey);

The Chrome Debugger informs: "Uncaught TypeError: this.getSelectedButton is not a function" as in this screenshot:

Now the difference (I believe) is that in your jsBin the function is declared right away in the constructor. In my case, the function is in the controller and the RadioButtonGroup is created in the xml.view.

How could I implement your code in my case? Thank you in advance,

Gabriel