How to get parent View/Fragment from a Control

cschuff picture cschuff · Oct 19, 2016 · Viewed 7.9k times · Source

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

BR Chris

Answer

schnoedel picture schnoedel · Oct 19, 2016

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.