cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

UI5 - Enable Button on XML View

Former Member
0 Likes
4,237

Hi All,

I have an XMLview in which I have a button on it, which is disabled. Defined using the following code: -

<Button id="rejectButton" enabled="false" text="Delete" type="Reject"

                        icon="sap-icon://delete" press="onDelete" />

The app is a split view. This button appears on the detail view. Now, I want to be able to click an entry in the master view and in turn enable this button.

I am failing miserably. I presume I may need to get hold of the button ID and somehow set the enabled to true. I am thinking I need to be using the sap.ui.getCore() thingy, but do not know how to specify an ID.


Could someone point me in the right direction of how to get the button element, so that I can enable it. Or if this is the wrong solution, tell me how I should be enabling it.

Thanks

Martin

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

Hi,

So this is possible programatically (but not recommended) by using


sap.ui.getCore().byId("idViewRoot--idViewDetail--rejectButton")

This should get you the button object.

Note that you have to specify the id of not only the button but also the view hierarchy it is placed withing (idRootView => idViewDetail) so if you move the button or change any higher up ids, then you have to change the code.

Using a local state JSON model, as suggested, is preferable and you can bind other things to the semantic action of enabling the button if needed with no more code. You also have the freedom to change the UI from the button logic and it makes use of the framework to do all the heavy lifting.

Hope this helps!

Oli

Qualiture
SAP Mentor
SAP Mentor
0 Likes

A bit off-topic, but as a reminder when using XML views you should use


this.getView().byId("rejectButton");

instead of


sap.ui.getCore().byId("the--whole--dreadful--view--layout--id--to--your--rejectButton");

However, of course the recommended approach is not to get a reference to the control and use the setEnabled() method, but to use a local JSON model to bind these UI properties as you and Jason Moors correctly stated

Former Member
0 Likes

Hi Robin,

Yes this is true but only if the required control is within the current view. The view.byId function searches only within itself. It actually prepends the current views' id to the id passed to that function and uses the sap.ui.core.byId function.

The OP wanted to change a control in a different view hierarchy and so the only way to do it by id is to specify the entire thing.

The best way is to use a little local JSON state model though as it puts all the hard work on the framework.

Oli

Answers (2)

Answers (2)

jmoors
Active Contributor
0 Likes

I normally just bind the enabled property to a model, and then update the model.

Here's a simple example.

http://jsbin.com/soqiluxedalo/1/edit

Regards,

Jason

Former Member
0 Likes

Quick update.

I "inspected" the element in Chrome and it had the ID "idViewRoot--idViewDetail--rejectButton".