Standard service - a fixed amount of work that is performed at a fixed price.  Post Service


Tuesday, 14 May 2019 14:34

SAPUI5 – Factory Functions for Beginners

Written by
Rate this item
(0 votes)

Are you one of them who is hearing Factory functions for the first time? Do not feel left out. Not many know about this term. Everyone is gradually

learning and evolving. In this tutorial, we would understand Factory Functions and perform a hands on exercise using Factory Functions.

Let check what SAP says about factory functions?

Instead of hard-codinga single template control, we will use a factory function to generate differentcontrols based on the data received at runtime. This approach is much more flexibleand allows complex or heterogeneous data to be displayed.

Let me make it simple for you….Factory in general terms, Consumes the raw material and Produces something good out of it.

So our main motive here is same …give our model data one byone to our factory function and thenbased on dynamic data we can display our controls or make our control moreappealing.

The factory function is a more powerful approach for creating controls from model data. The factory function is called for each entry of a control’s aggregation, and the developer can decide whether each entry shall be represented by the same control with different properties or even by a completely different control for each entry.

Let me consider a case where we want to display a table with one column where it will highlight the row or display some more control in same column.

The factory function comes with the parameters sId, which should be used as an ID for the new control, and oContext, which is for accessing the model data of the entry. The returned object must be of type sap.ui.core.Element.

Also Check : Free Video Tutorial on SAP OData End to End

Exercise Time!!

Enough of theory part: let’s produce something out of this factory. ?

Create a project using template.

STEP1: Create UI5 Project from Template

Create project from template -> select SAPui5 Application.

Enter view details.

Click on finish.

STEP2: Create one more View.

STEP3: Configureroutes, routing and target in manifest. json:

RootView :

Routing and target:

Also Read:Add Delete & Save Multiple Records in SAPUI5. Part 1 – ADD

STEP4: Configuredata model:

4.1 Create a filein under Model folder:

4.2 configure ourmodel file in manifest.

As highlighted below.

Since I gave my model name m1 we will use it in view to display data.

Let’s make few changes in our two viewS which we have created:

  1. root_view
  2. page1

My root_view lookslike this:

My page1 viewlooks like this.

So I have specified the path for item and the output willbe.

Now apply our factoryfunction in view:

The requirement here is set color in Quantity column based on Unit price value. If the Unit price is more than 20 then display Quantity as red otherwise display as green.

Something like this.

First step is to create style to highlight.

Make changes in View:

Path: Indicates the path for aggregation binding.

Factory: .Myfactoryfunction (create this function in page1’s controller with two parameters)

Note: Did you notice we have commented our Item bindings from View (lines 31 and below) … Why?

Because we will writethe logic in our factory function.

Lets call this function in controller.

Also Check:An ABAPer’s Second SAPUI5 App

Put a debugger to check whether our factory function is working or not.

Cheers!!! It is going for first record.

Now you might face an error because factory function has a return parameter ….remember I have said factory is meant to produce something good from raw material.

So lets modify our code in our Myfactoryfunction in controller, bind our data here and see how it checks for one by one record.

Change the controller :

Here I have the parameter Ocontext which holds property value and we can compare this with some other property or static value like I have used 20 or more for unit price. The code above in IF ELSE clause is self-explanatory.

Check the output:

Upload this full project of this factory function in your system and test it end to end.

Comments Please!!

If you want to have real time discussions and resolutions, do join our SAP Technical Telegram Group where we have more than 4745+ active SAP consultants from 6 Continents.

Please Note: You need to install Telegram App on your mobile first and then you can join the group using the above link.

Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.

Step by Step Tutorials on SAPUI5

Read 178 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.