Pablo Garcia
Pablo Garcia

Reputation: 381

Get element from XMLview inside NavContainer

I have the next structure

<NavContainer id="navContainer" width="100%" height="100%">
    <mvc:XMLView id="home_id" viewName="my.app.Home"/>
    <mvc:XMLView id="detail_id" viewName="my.app.Detail"/>
</NavContainer>

Inside the detail view I Have a list like:

<List
    id="list_id"
    width="85%"
    visible="true">
       ...
       ...
</List>

and when I want to get this list using the code below I get an "Undefined" :

sap.ui.getCore().byId("list_id");

I check the Dom the id is:

__xmlview1--detail_id--list_id

Can someone helps me? Thank you!

Upvotes: 0

Views: 329

Answers (1)

Rahul Bhardwaj
Rahul Bhardwaj

Reputation: 2353

Since you are using XML views, the ID of the controls get are prefixed with view ID in which they are created. This way same Ids can be used in different views. Hence, the ID of your list is : detail_id--list_id ( detail_id is the id you provided while embedding the view). Again, since detail_id has been embedded in an XML view, parent View Id is prefixed. with detail_id. So complete ID becomes: __xmlview1--detail_id--list_id. Here, __xmlview1 is autogenerated ID by the framework. In JS view, ids are never prefixed and hence you can use the method: sap.ui.getCore().byId('id_here'). However, as IDs are prefixed in XML view, you will need to use the method: this.getView().byId('id_here'), where this is the controller, and therefore, we are using the byId method of the View and not of sap.ui.core library.

To resolve you issue, use the below code:

this.getView().byId('detail_id').byId('list_id')

Lets break this down: 1. First fetching the view which has your list: so using the byID method of the View ( since XML View is used), this.getView().byId('detail_id'). 2. Now that we have the detail view, we can use it to fetch the list using the byId method of detail_view. Using the chaining convention we get full code:

this.getView().byId('detail_id').byId('list_id')

Let me know if this clears your doubts. :)

Upvotes: 3

Related Questions