KcH
KcH

Reputation: 3502

Manipulating control accessed with "byId" has no effect

I have a sample of icons in a HBox in an XML view as below:

<m:HBox>
  <core:Icon src="sap-icon://show" color="#007bff" id="test1"  press="onPressView" />
  <core:Icon src="sap-icon://edit" color="#007bff" id="test2" press="onPressEdit" />
  <core:Icon src="sap-icon://print" color="#007bff" id="test3" press="onPressPrint" />
</m:HBox>

In controller to set visible false, I am doing as:

let testIcon1 = this.getview().byId('test1');
testIcon1.setVisible(false);

But this isn't working.

Upvotes: 0

Views: 359

Answers (1)

Boghyon Hoffmann
Boghyon Hoffmann

Reputation: 18044

You're probably accessing a template control instead of the actual rendered one. If you inspect the element in the devtools, see if the ID of the rendered control (Icon) has the suffix __clone<n>.

Since such ID suffixes are generated by the framework, accessing cloned controls via byId should be avoided. There are many alternative solutions in such cases. A general good solution is to separate views from the controller by binding the control properties (e.g. visible) and manipulating those properties from the model.

Upvotes: 2

Related Questions