Skip to Content
Author's profile photo Former Member

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.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jun Wu
      Jun Wu

      byid not working? are u sure?

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      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.

      Author's profile photo Jun Wu
      Jun Wu

      not following u. if you assign an id to the control? you cannot  get it by using byid?

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Jun Wu
      Jun Wu

      you should use this.getView().byId()