Skip to Content

How to access a control with dynamic id

A control with dynamic id, for example “__component0—sap.app.view.MainView–sap.app.view.TableView–Table1”, which changes in runtime and therefore this control cannot be accessed by fynction byId, can be however accessed in following way:

if for example there is the view with the table:


<mvc:View
  displayBlock="true"
  xmlns:l="sap.ui.layout"
  xmlns:table="sap.ui.table"
  xmlns:mvc="sap.ui.core.mvc"
  controllerName="sap.app.controller.TableView"
>
  <l:HorizontalLayout>
   <SearchField liveChange="onSearch"/>
  </l:HorizontalLayout>
  <table:TreeTable
    id="Table1"
    rows="{/root}"
  >
    <table:columns>
      ...
    </table:columns>
  </table:TreeTable>
</mvc:View>

then in the controller of this view (sap.app.controller.TableView) this table can be accessed by following statement:


var oTable = this.getView().getContent()[1];
oTable.expandToLevel(3);

or in other controller this table can be accessed by calling corresponding function of this controller:


expandTable: function () {
var oTable = this.getView().getContent()[1];
oTable.expandToLevel(3);
}

See How to call function of one controller from another controller for more details.

5 Comments
You must be Logged on to comment or reply to a post.
    • I didn’t say that byId not working, i said that control cannot be acessed by this function, because i cannot use this function for searching by id when id changes.

        • Exactly! As you can see above, the id “Table1” is assigned to TreeTable control, but this control cannot be found by sap.ui.getCore().byId(“Table1”), because, as it also written above, in runtime id of this control is not “Table1“, but “__component0—sap.app.view.MainView–sap.app.view.TableView–Table1” (and in another view this control has another id):

          “it’s not actually possible to have stable IDs for controls in bound aggregations”

          See SAPUI5 SDK – Demo Kit for more details.