2

I have a Dialog in fragment:

<core:FragmentDefinition
    xmlns="sap.m"
    xmlns:f="sap.ui.layout.form"
    xmlns:core="sap.ui.core">
    <Dialog title="{i18n>AddNewItem}" resizable="true" draggable="true">
        <content>
            <MessageStrip
                id="failMsg"
                visible="false"
                text="{i18n>SensorTypesCreateFail}"
                type="Error"
                showIcon="true"/>
    </Dialog>
</core:FragmentDefinition>

As in UI5 doc

Retrieving a control instance when the fragment is not part of a view:

  • When no fragment ID was given: myControl = sap.ui.getCore().byId("myControl")

  • When a fragment ID myFrag was given: myControl = sap.ui.core.Fragment.byId("myFrag", "myControl")

If there is no visible="false", I can get this MessageStrip by sap.ui.getCore().byId("failMsg").

But I found that with visible="false", id of MessageStrip is sap-ui-invisible-failMsg, I failed to found proper API to get it.

Of course I can use sap.ui.getCore().byId("sap-ui-invisible-failMsg"), but I am not sure whether this ID will change after I deploy it to FLP, and as @schnoedel said in another question:

Beware that the prefixes like -- and --- used by the framework may change in the future. Thats why it's recommended to use the public api functions the framework supplies like byId() and createId().

So, is there any better way to get it?


Update:

Change my code from:

this[dialogName] = sap.ui.xmlfragment("namespace." + dialogName, this);
this.getView().addDependent(this[dialogName]);

To

this[dialogName] = sap.ui.xmlfragment(dialogName, "namespace." + dialogName, this);
this.getView().addDependent(this[dialogName]);

And now my id is sap-ui-invisible-dialogName--failMsg...

Community
  • 1
  • 1
Tina Chen
  • 2,010
  • 5
  • 41
  • 73

2 Answers2

0

It depends on what you want to achieve after getting the ID. If you just want to change a property you could do it without any ID via a Model.

For that you can assign a Model field (i.e. baseModel>/visable) to the visable property and once it should be changed you change the model and via two way binding it updates the control. code to change a model: this.getView().getModel("nameOfUrModel").setProperty("property", "value")

for more information about this just check the walkthrough tutorial on https://sapui5.hana.ondemand.com/

And if you for whatever reason really need the ID: https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.core.Fragment.html here you find the command: sap.ui.core.Fragment.byId(sFragmentId, sId) It should be able to return the Control your using

Hope that helps Eric

Erch
  • 589
  • 5
  • 25
  • https://github.com/SAP/openui5/blob/master/src/sap.ui.core/src/sap/ui/core/Fragment.js#L177 It seems ` sap.ui.core.Fragment.byId(sFragmentId, sId)` equals to `sFragmentId + "--" + sId`, It did not work for id `sap-ui-invisible-failMsg`, since it is not `myFragmentId--failMsg` – Tina Chen Aug 22 '17 at 06:14
0

You were very close to the solution. After adding dialogName for the fragment ID in its creation, you just have to call the API ...:

sap.ui.require(["sap/ui/core/Fragment"], Fragment => Fragment.byId(dialogName, "failMsg"));

... to get the control instance as mentioned here.

However, regardless whether you provided a fragment ID or not, you can easily ignore the render prefix "sap-ui-invisible-" at any time - Meaning that you could've also been able to get the control instance via sap.ui.getCore().byId("failMsg") instead of sap.ui.getCore().byId("sap-ui-invisible-failMsg") even if the control is invisible.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170