Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 

As name indicates factory is the place where something good will be produced  by consuming the raw material.In the same way factory function is used to produce the controls in UI5.The objective of this blog is to explain the usage of factory function in easy way with example.

Factory function is the most powerful approach for creating the controls dynamically by using model data in UI5.

Instead of hard coding the control in view we can dynamically generate the ui5 controls in controller according to data we got from the backend system.Factory function will be looped for each and every item in the array and controls can be created accordingly.

It has 2 parameters.

1)sId : This will be used as id for the newly created control.

2)oContext : This parameter can be used to access the model data which we got from backend system.Each row’s context value can be read and control will be created according to the model value.


In this example we will create  the rows for sap.m.table dynamically.

We will use a JSON array with properties name,city and amount to bind to our table.
onInit: function(){
var arr = [{
var demoModel = new sap.ui.model.json.JSONModel({
"results": arr

Our view code will be as below
<Table visible="true" id="ID_DEMO"                                   
items="{ path:'demoModel>/results',factory:'.myFactory'}">
<Column minScreenWidth="Tablet" demandPopin="true">
<Text text="Name" />
<Column minScreenWidth="Tablet" demandPopin="true">
<Text text="City" />
<Column minScreenWidth="Tablet" demandPopin="true">
<Text text="Amount" />

In this example  we will create dynamic control to show amount field based on the data from the model. If amount value is greater than 0 we will create Text control otherwise we will create Input control for showing amount  field.

Our factory function with 2 parameters will be as below.
myFactory :function(sId,oContext){
var Value = oContext.getProperty("Amount");
var element;
if(Value > 0){
element = new sap.m.Text({
element = new sap.m.Input({

return new sap.m.ColumnListItem({
cells:[new sap.m.Text({
new sap.m.Text({

The resultant table will be created as below.


By using factory functions we can make our UI more dynamic.We can decide which control to use by reading the back end data create our control accordingly.
Labels in this area