3

I am creating a SAP Fiori application. I have input in a dialog box in that I have to fetch the input value. I am defining the dialog in fragment view.

When I try to give the id for input I am getting an error as adding element with duplicate id.

------ Fragment View------

<core:FragmentDefinition
  xmlns="sap.m"
  xmlns:core="sap.ui.core"
  xmlns:app="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1">
  <Dialog title="Title"  class="sapUiPopupWithPadding" >
    <content>  
        <HBox> 
           <items> 
              <Text  text="Name"></Text> 
              <Input  value="" id="myId"  > </Input> 
           </items> 
       </HBox> 
    </content>
    <beginButton>
        <Button text="Ok"  press="DialogButton" />
    </beginButton>
</Dialog>

---Controller Code---

DialogButton:function(oEvent) {

   var myIdValue=sap.ui.getCore().byId("myId").getValue();

   console.log("ID Value :::"+  myIdValue);

   oDialogFragment.close();

}

enter image description here

Saddamhussain
  • 860
  • 5
  • 14
arunk
  • 185
  • 2
  • 4
  • 14
  • 1
    As an aside, if you're really creating an SAP Fiori app, you ought to avoid using sap.ui.getCore().byId. – qmacro Aug 19 '14 at 05:33

3 Answers3

8

You create a new dialog fragment instance every time you need to open the dialog . This will cause the duplicated ID issue. Please keep a dialog fragment instance in your controller.

Please see the sample code:

DialogButton:function(oEvent) {
   if(!this.oDialog) {
      this.oDialog =  sap.ui.xmlfragment("you.dialog.id", this );
   }
   this.oDialog.open();
}
Haojie
  • 5,665
  • 1
  • 15
  • 14
  • But in this way, fragment will keep the value/state when closed every time it opens. Even if I destroyed it , I still can not NEW an instance? – Tina Chen Oct 10 '17 at 09:25
  • https://stackoverflow.com/questions/46484467/how-to-clear-dialog-xmlfragment-content-after-close Is there any suggestion? – Tina Chen Oct 10 '17 at 09:28
2

take a look at the following help IDs in Declarative XML or HTML Fragments you need to add an ID when the fragment is instantiated, that way the control has a prefix which is unique

Jasper_07
  • 2,453
  • 2
  • 18
  • 23
1

It is also a good idea to add your fragment as dependant to your main view. This way it will be destroyed when the main view is destroyed. And you will not get duplicate id error when navigating away from your view and back.

DialogButton:function(oEvent) {
   if(!this.oDialog) {
      this.oDialog =  sap.ui.xmlfragment("you.dialog.id", this );
      this.getView().addDependent(this.oDialog);
   }
   this.oDialog.open();
}
Nikolay
  • 11
  • 1