How to dynamically load an XML fragment as XML?

Suppose I have the following kind of XML:

<core:View xmlns:core="sap.ui.core" ...> <Page> <content> <l:VerticalLayout> <l:content> <core:Fragment fragmentName="my.static.Fragment" type="XML" /> </l:content> </l:VerticalLayout> </content> </Page> </core:View> 

The my.Fragment statically loaded. However, now I want to dynamically change the loaded fragment (ideally, using the data binds the fragmentName property, but any other means should also be in order), i.e. something like that:

 <core:View xmlns:core="sap.ui.core" ...> <Page> <content> <l:VerticalLayout> <l:content> <core:Fragment fragmentName="{/myDynamicFragment}" type="XML" /> </l:content> </l:VerticalLayout> </content> </Page> </core:View> 

However, the latter does not work, and fragment definitions do not allow data binding ... Maybe I missed something, but how should I dynamically change the fragment in my XML representation based on the property of the parameter / model / etc.?

Currently, I resorted to a custom control instead of directly using a fragment in my view, and this control dispatches to the corresponding fragment, but I think it should be simpler, box way ...

+3
source share
2 answers

I think the only solution would be to initialize the fragment from the onInit controller method:

 sap.ui.controller("my.controller", { onInit : function(){ var oLayout = this.getView().byId('mainLayout'), //don't forget to set id for a VerticalLayout oFragment = sap.ui.xmlfragment(this.fragmentName.bind(this)); oLayout.addContent(oFragment); }, fragmentName : function(){ return "my.fragment"; } }); 
+4
source

A fragment name can also be associated with a binding, including the binding of an expression, which is evaluated by a constant. Since formatting functions return strings, not booleans, === 'true' was added in the following example:

Example: dynamic fragment name

 <core:Fragment fragmentName="{= ${path: 'facet>Target', formatter: 'sap.ui.model.odata.AnnotationHelper.isMultiple'} === 'true' ? 'sap.ui.core.sample.ViewTemplate.scenario.TableFacet' : 'sap.ui.core.sample.ViewTemplate.scenario.FormFacet' }" type="XML"/> 
-1
source

Source: https://habr.com/ru/post/1210677/


All Articles