cancel
Showing results for 
Search instead for 
Did you mean: 

How to get an Control using the byId? But the Control is inside a list, and this list is inside a fragment.

0 Kudos

Hi, How to get an Control using the byId? But the Control is inside a list, and this list is inside a fragment. This is the code to add the fragment:

This is the fragment:

In the controller, all of these return undefined:

this.getView().byId("myIpt")

sap.ui.core.Fragment.byId("myFragment","myIpt")

sap.ui.getCore().byId("myIpt")

* As you can see in the input I have add an event the liveChange, with a breakpoint I can see that the input Id is:

oControlEvent.getSource().getId() === "__xmlview3--create--myFragment--myIpt-__xmlview3--create--myFragment--myList-0"

The only way I found to get the input was by removing the id from the fragment ("myFragment") and with the following:

this.getView().byId("myList").getItems()[0].getContent()[0].getItems()[2]

Bu in my opinion this is going to give me a lot of headaches in the future with maintenance.

What I want to do is something similar to "byId", but it is not working, so I need help to understand why this does not work, and directions to the best solution.

thanks Cristiano.

Accepted Solutions (1)

Accepted Solutions (1)

kedarT
Active Contributor
0 Kudos

Hi Cristiano,

You are unable to access the Input field using byId as depending on the data in the List there will be multiple instance of the Input field i.e.  myList-0 .....myList-9 and so on.

The only way to get the value in the Input field will be  as mentioned by you as below:

this.getView().byId("myList").getItems()[0].getContent()[0].getItems()[2]


Hope this helps

0 Kudos

Kedar, you are fast. thanks. well, it is not the ideal, like I sad I am afraid of the maintenance, once that if a developer adds a new control in between, it is going to break it. Thanks, Cristiano.

0 Kudos

hi,

to solve that problem I have used sap.ui.core.Fragment.byId(fragmentId, controlId);

thanks to all for the tips, Cristiano.

Answers (2)

Answers (2)

former_member182862
Active Contributor
0 Kudos

Hi Cristiano

Since you have a input in a item in a list, depending on the number of items in the list, there are going to be many instances of the input. For instance, you have 10 items in the list and you are going to have 10 inputs. Do you want to get the 10 input controls?

Thanks

-D

saivellanki
Active Contributor
0 Kudos

Hi Christiano,

For Dynamic, you could use a jQuery to get control Id.

I tried with selectionChange event of a list and input ID of custom list Item, Check the sample here - sap.m.List Input ID Sample


And the code will be for you -



selectionChange : function(oEvent){

  var oSelectedIndex = oEvent.getParameter("listItem").getId().slice(-1);

  var oSelectedInputId = $('#myList').find('.sapMInput')[oSelectedIndex].id; //Your selectedInputID

  alert(oSelectedInputId);

  alert(sap.ui.getCore().byId(oSelectedInputId).getValue());

  }


But, I was not sure about the scenario, like you need it in selection event or select of some button. However, the above logic works for any scenario just some tweak required on how you use it.


Regards,

Sai Vellanki.