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

穿精又带淫゛_ 提交于 2020-01-25 02:59:04

问题


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?


回答1:


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

  • getOwnerComponent()
  • byID

Managed Object

  • getAggregation()

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




回答2:


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");



回答3:


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
    },
}


来源:https://stackoverflow.com/questions/36529014/how-to-get-the-reference-id-of-an-element-in-another-view-ui5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!