Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically load an XML fragment in XML view?

Tags:

sapui5

Suppose I have the following XML view:

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

The fragment my.Fragment is statically loaded. However, I now want to dynamically change the to-be-loaded fragment (ideally using data binding the fragmentName property, but any other means should be ok as well), ie. something like this:

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

However, the latter does not work, and the Fragment definitions don't allow for data binding... I might have missed something, but how should I dynamically change the Fragment in my XML view based on a parameter/model property/etc?

For now, I have resorted to a custom control instead of directly using a fragment in my view, and have that control do the dispatching to the appropriate Fragment, but I feel there should be an easier, out-of-the-box way...

like image 557
Qualiture Avatar asked Sep 06 '14 12:09

Qualiture


People also ask

How to load fragments dynamic based on model data in XML binding?

How to load fragments dynamic based on model data in XML Binding? There is an XML view. In a block of panel i should call another fragment inside but based on Model Data. If there are 5 items in drop down, based on selection one fragment should display in view.

How to create a view and fragment from an XML template?

The XML preprocessor is used to create a view as well as fragments. Following code snippets will describe how to create a view and fragment out of XML Template. // Code is written in onAfterRendering of controller of view where you want to use this template as a fragment.

How to dynamically add fragments to an existing layout?

To dynamically add fragments to an existing layout you typically define a container in the XML layout file in which you add a fragment. For this you can use a framelayout as in the following:

How do I return a collection from an XML fragment?

In the custom axis method, after you create the XML fragment by calling the ReadFrom method, return the collection using yield return. This provides deferred execution semantics to your custom axis method. When you create an XML tree from an XmlReader object, the XmlReader must be positioned on an element.


1 Answers

I think the only solution will be initialization of fragment from onInit method of controller:

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";
    }
});
like image 71
Nikolay Nadorichev Avatar answered Oct 18 '22 02:10

Nikolay Nadorichev