Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent View/Fragment from a Control

How can I retrieve the View/Fragment a sap.ui.core.Control belongs to?

BR Chris

like image 698
cschuff Avatar asked Dec 19 '22 12:12

cschuff


2 Answers

You can walk up the parents until you find the View. You should however not rely on identifiers. Use the Class or Metadata to identify a View:

  buttonPress: function(oEvent){
    var b = oEvent.getSource();
    while (b && b.getParent) {
      b = b.getParent();
      if (b instanceof sap.ui.core.mvc.View){
        console.log(b.getMetadata()); //you have found the view
        break;
      }
    }
  }

Example on JSBin.

Fragments are not added to the control tree. So you cannot find them. You can however find the view they have been added to.

like image 100
schnoedel Avatar answered Dec 28 '22 19:12

schnoedel


If the identifier of your control contains the identifier of the View (something like "__xmlview42" if you are using XML views) you could extract it and call:

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

to get the containing view. If the identifier is not present you can navigate through the control tree using:

control.getParent()

until you have a control whose identifier contains the View identifier. You could also navigate through the control tree until you reach the view.

For Fragments this won't work as the content will become part of the parent View.

like image 26
matbtt Avatar answered Dec 28 '22 20:12

matbtt