Takao Baltazar
Takao Baltazar

Reputation: 329

How to get the reference id of an element in another view - UI5

How can I get the reference id of an element in another view? Like for example I have the following file structure (mvc)

-view
|-- View1.view.xml
|-- View2.view.xml
-controller
|-- View1.controller.js
|-- View2.controller.js

Assuming that during runtime of View2.controller.js, how can I get a certain element in View1.view.xml?

Upvotes: 1

Views: 7001

Answers (3)

inetphantom
inetphantom

Reputation: 2594

You can use relative navigation. Do not use absolute Navigation, since that will not work anymore if you change like from local html to a FioriLaunchpad.

Commands you might want to look at:

Controller

Managed Object

Now you can navigate to the OwnerComponent, and there will be saved your target View, either as variable or as aggregation of your Element.

Upvotes: 1

Tina Chen
Tina Chen

Reputation: 2040

For example, I am trying to get an element which id is idAppControl in App.view.xml, and I want to access it in Master.controller.js

Here is my solution:

var ownerId = this.getView()._sOwnerId,
rootId = this.getOwnerComponent().getManifestEntry("sap.ui5").rootView.id,
id = "idAppControl",
realId = ownerId + "---" + rootId + "--idAppControl",   
element = sap.ui.getCore().byId(realId);

sOwnerId is a private property, I failed to find a better way to get it.

My manifest.json:

"sap.ui5": {
    "rootView": {
        "viewName": "xxx.view.App",
        "type": "XML",
        "id": "app" // what I get in rootId
    },
}

Upvotes: 0

Ajay Reddy
Ajay Reddy

Reputation: 1493

Let's say the ID of the element in View1.view.xml is "idOfElement". you can access the reference of that element in another view(View2.controller.js) of the same application using the statement:

                         var elementID = sap.ui.getCore().byId("idOfElement");

Upvotes: 0

Related Questions