cancel
Showing results for 
Search instead for 
Did you mean: 

[UI5] How to deal with duplicated ID error?

super_yan
Advisor
Advisor
0 Kudos

I have a fragment: MyFragment. Sample code is:

// MyFragment
<Label id="my_label_id" text="my label" />

My fragment is contained by other views:

MyView contains both Fragment1 and Fragment2, and Fragment1 and Fragment2 contains MyFragment.
In MyView, there are 2 MyFragment, so it contains 2 Label with same ID: "my_label_id". This leads to a duplicated ID error.
How to deal with this error?

View Entire Topic
AlexNecula
Active Contributor
0 Kudos

Not sure why you want to do this. IDs are meant to be unique.

Maybe you want to use this.getView().byId("my_label_id") irrespective of what fragment was opened or something similar. I suggest to just check what was opened and select the correct ID.

super_yan
Advisor
Advisor
0 Kudos

This is the requirement. I need to add some labels to MyView. If MyView is in readonly mode, Fragment1 is show, Fragment2 is hidden; And If MyView is in edit mode, Fragment1 is hidden, Fragment2 is show.

I created MyFragment which contains all the new labels, and add it to both Fragment1 and Fragment2. Then get this error.

AlexNecula
Active Contributor
0 Kudos

I would try to use the destroy() method on Framgent1/Fragment2 when switching between display/edit.

Another option that still doesn't require having the same fragment duplicated would be to add the labels through javascript. I would probably prefer this one since with destroy() the fragments will have to be initialized every time.

super_yan
Advisor
Advisor
0 Kudos

Yes, "add labels through javascript" may work. Thank you!