Dynamic text in SAP Fiori Fragments



Hello every one, In our earlier tutorial we have introduced you to the concept of Fragments in SAPUI5/Fiori. If you have missed it,click here. In our last tutorial we have defined static text/data which are used in Fragments, in this tutorial we will try to push the text dynamically. Let see how we can do this. This is continuation to our earlier tutorial on Fragments,please refer to it before proceeding further.

1.  Follow the steps which exists in our last tutorial.

2. Revisit the code inside the Fragment file. We had declared a static text for the Text inside the Dialog like below.

Fragment in SAPUI5




3. Here we will dynamically pass the text to the fragments from the view controller. Here we are going to use the concept of model binding to achieve this.Go back to the view controller and write the below code to declare a model and add data to the model.


sap.ui.controller("demofragments.MainView", {
    onInit: function(){
	// set model, which is used in Fragments.
	oModel = new sap.ui.model.json.JSONModel({
		myText : "Say,Hello to Fragments in SAPUI5"
	sap.ui.getCore().setModel(oModel, "myData");
    onPress : function() {
	this._Dialog = sap.ui.xmlfragment("demofragments.fragments.Dialog",
    onClose : function() {

4. What have we done onInit() method in previous step, we have declared a JSON model and set the data to the model and assign the model to the sap.ui.getCore() for globally access across the application.


5. Now go back the dialog fragment file and slightly change the code to get the text from model.Copy and paste the below code.

<core:FragmentDefinition xmlns="sap.m" xmlns:l="sap.ui.layout" xmlns:core="sap.ui.core">
	<Dialog title="My Dialog">
			<Text text="{myData>/myText}" />  
			<Button text="Close" press="onClose" />

6. Now save the changes and test the application, output of the application should look similar to below.

Using bindings in Fragments







7. The text displayed inside the popup dialog should be same as the text defined in the model in onInit() method of the view controller.

By this we will conclude this tutorial. Stay tuned to us for more SAPUI5 tutorials 🙂

Feel free to comment. Thank you….!

WordPress database error: [Table 'saplearn_wp1.wp_nsex_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_nsex_comments.comment_ID FROM wp_nsex_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1045 AND comment_parent = 0 ORDER BY wp_nsex_comments.comment_date_gmt ASC, wp_nsex_comments.comment_ID ASC