Professional Documents
Culture Documents
Sapui5 PDF
Sapui5 PDF
Sapui5 PDF
SAP UI
1
Agenda
SAPUI5 Overview Databinding
Runtime Resources Styling & Theming
Programming Applications Extending SAPUI5
SAPUI5 Developer Studio Notepad Controls
Creating a SAPUI5 Project CVOM Charts
Resource Handling / Localization Optimizing SAPUI5
MVC
SAP UI5 Overview
SAP UI
3
SAP UI Development Toolkit for HTML5
SAPUI5
The short name
UI5
Internal name
UI5 used to be named "Phoenix", it was changed to SAPUI5 in the summer of 2011 because the name
Phoenix was not suitable to be used publicly.
Main UI5 Characteristics
UI5 Core
UI5 Core includes base, core and model
modules
Dependency / Class-Loader to load
control libraries
Render manager creates HTML strings
for the instantiated controls
The UI5 Core includes other JavaScript
libraries
jQuery
jQuery UI
data.js
UI5 Control Libraries
sap.ui.commons
Includes bread and butter"
controls like TextField,
TextView, Button
sap.ui.ux3
Includes UX3 patterns, mainly
available in Gold
Reflection design
e.g.: Shell, ExAct and Thing
Inspector
UI5 Control Libraries
sap.ui.table
Includes DataTable control
sap.ui.dev
Experimental controls
not included in all UI5
offerings
UI5 and Extensibility
Include their own JavaScript, HTML and CSS into UI5 based pages
This way UI5 development groups should not become a bottleneck for application groups in need of a certain functionality.
UI5 Knowledge and Test Resources
Demo kit
API Documentation
SAPUI5 Versions
Test Suite
Exercise Create a Simple Application
Add the Labels, the TextFields and the Button to the layout with the
createRow() method
Bonus: Add a press handler to the Button that displays the values of
the TextFields.
attachPress(function() {})
getValue()
Solution to Exercise
var oLayout = new var oButton = new
sap.ui.commons.layout.MatrixLayout({ sap.ui.commons.Button({
width: "100%", text: "Submit"
widths: ["20%", "80%"] });
});
var oLabel1 = new sap.ui.commons.Label({ // add rows with label/textfield to
text: "Given Name" table
}); oLayout.createRow(oLabel1, oTextField1);
var oTF1 = new oLayout.createRow(oLabel2, oTextField2);
sap.ui.commons.TextField({ oLayout.createRow(null, oButton);
id: "given-name", oLayout.placeAt("content");
width: "100%"
}); // add handler to alert textfield values
var oLabel2 = new sap.ui.commons.Label({ oButton.attachPress(function() {
text: "Last Name"
}); var msg = "TextField1: "
var oTF2 = new + oTF1.getValue() + "\n"
sap.ui.commons.TextField({ + "TextField2: " + oTF2.getValue();
id: "last-name", alert(msg);
width: "100%" });
});
Runtime Resources
How to get UI5 resources
UI5 mainly consists of JavaScript, CSS and image files that run in a browser.
Apart from this main offering the runtime files UI5 has many more optional pieces of software
Runtime documentation
Eclipse development tools
Sample apps
In order to get UI5 running on a web page, the UI5 resources need to be loaded by the browser.
These options are available for referencing the UI5 runtime files:
CDN (content delivery network)
Runtime libraries (wars and jars)
SDK
Public Trial
A trial version of the SAPUI5 framework has been released in the SAP Community Network
http://scn.sap.com/community/developer-center/front-end
UI5 pages always have to start with the bootstrap, to initializes the UI5 runtime.
Attributes of the script tag are evaluated and used to configure the runtime
data-sap-ui-libs: the controls libraries to be used, comma-separated
data-sap-ui-theme: the theme
There are more attributes: data-sap-ui-language, data-sap-ui-rtl,
Instead of putting the attributes in the script tag, they can also be added as URL parameters
Application script and UI area
Remove removes the control from the tree it is removed from the UI yet the control instance
still lives and can be reused at a later point in time
Destroy removes the control from the tree and the UI but also the control instance in JavaScript
is freed.
If you want to reuse a control at a later point in time you should keep a reference in a variable and just
remove it.
If you dont need the control anymore, you should destroy it in order to free the resources.
Useful Core functions
sap.ui.getCore() jQuery.sap.domById(id)
get a core instance gets any HTML element with id id
If there is also a UI5 control with id id, the
sap.ui.getCore().byId(id) element returned is the topmost HTML
gets an instance of a UI5 control which element of this UI5 control
was created with id id The topmost HTML element of a UI5
control always has the id defined for the
can be used to retrieve removed UI5 control
controls (even though the id doesnt
jQuery.sap.byId(id)
exist in the DOM anymore)
returns the jQuery object of the DOM
sap.ui.getCore().applyChanges() element with the specified id
carries out and renders the changes for similar to document.getElementById but
UI5 controls directly, so before the gets rid of IE bug which also retrieves
runtime would do it elements with the name of id
Exercise getCore().byId()
Instantiate a MatrixLayout
new sap.ui.commons.layout.MatrixLayout
oMatrix.createRow(oTextField, oButton);
oMatrix.placeAt("content");
</script>
SAPUI5 Developer Studio
Overview
SAPUI5 Developer Studio is a set of tools for Eclipse that greatly simplify the development process for
UI5 applications and controls, including:
Wizards for project and view/controller creation
Wizards for control development
Code Completion for UI5 controls
TeamProvider for NGAP and BSP repositories
Application preview with an embedded Jetty server
Proxy-servlet to prevent cross-site-scripting errors
More detailed information about the SAPUI5 Developer Studio is available in the SAPUI5 Tools Wiki at
tools.hana.ondemand.com
Installation
Prerequisite: Install the latest Java Development Kit 6, if it is not installed already.
Download and install one of the preconfigured IDEs. If you want to use the SAPUI5 Repository Team
Provider you should choose the 32bit version.
It is also possible to setup Eclipse on your own by using one of the available update sites for Eclipse.
Once Eclipse has been (re)started, the SAPUI5 Eclipse tools should be available. One way to confirm a
successful installation is to check whether you can create a UI Library Project / SAPUI5 Application
Project.
The full installation information is available in the Tools Wiki at:
http://scn.sap.com/community/developer-center/front-end/blog/2013/06/01/how-to-install-a-basic-
development-environment-for-sapui5
Creating a UI5 Project with SAPUI5
Developer Studio
Creating a new SAPUI5 Project
Get the resource bundle for a given language (if no locale is given, English is
loaded by default)
jQuery.sap.require("jquery.sap.resources");
var oBundle = jQuery.sap.resources({url : sUrl, locale: sLocale});
The UI5 framework has built in support for modularizing larger JavaScript
applications.
Instead of defining (and loading) one large chunk of JavaScript code, an application can be split
into smaller parts, which then can be loaded at runtime when they are needed. These smaller,
individual files are called Modules in UI5.
To load a module, the function jQuery.sap.require must be used.
In a module name all . are replaced by / and an .js extension is added to create a path.
So sap.ui.commons.MessageBox will become sap/ui/commons/MessageBox.js
This is an example:
<script>
jQuery.sap.require("sap.ui.commons.MessageBox");
function onPressButton() {
sap.ui.commons.MessageBox.alert("Hi World!");
}
</script>
Modularization using declare
In order to create your own JavaScript Module you need to declare the module
A file becomes a module by calling the jQuery.sap.declare function. This tells the UI5
runtime about the name of the module. UI5 runtime keeps track which modules are
already loaded.
If a module is required (jQuery.sap.require) and it hasnt been loaded before, it is loaded
automatically. While it is carried out, it calls the declare method, so from now on UI5
knows that it has been loaded and when the next require comes about nothing needs to
be done anymore
The declare function checks if the parent namespace object exists and if not, creates it
// declaration of the module. Will ensure that the namespace 'my.useful' exists.
jQuery.sap.declare("my.useful.SampleModule");
Address.controller. sap.ui.controller("sap.hcm.Address", {
onInit: function() {
js: this.counter = 0;
},
sayHello: function() {
alert("Said hello "
+ this.counter++ + " times.");
}
});
<core:View xmlns:core="sap.ui.core"
xmlns="sap.ui.commons"
Address.view.xml: controller="sap.hcm.Address">
<Panel>
<Button press="sayHello" text="Say Hello" />
</Panel>
</core:View>
sap.ui.xmlview("sap.hcm.Address").placeAt("uiArea"
);
Location logic
Controllers and views use the require/declare logic, so if a controller is referenced like this:
<core:View xmlns:core="sap.ui.core" xmlns="sap.ui.commons"
controller="sap.hcm.Address">
UI5 then checks if you already have defined the controller like this in one of your already processed
application sources
sap.ui.controller("sap.hcm.Address", {
// controller logic goes here
});
If this is not the case then UI5 tries to load this definition from a file that by default is located in your
UI5 resources folder. This applies to views as well.
resources/sap/hcm/Address.controller.js
Controller Events
Currently there are 4 events defined in a You can add handlers for any of these
controller:
for to your controller class:
onInit
sap.ui.controller("sap.hcm.Address", {
fired when the view is first instanciated
onInit: function() {},
onBeforeRendering onBeforeRendering: function() {},
Fired before the view is rerendered onAfterRendering: function() {},
onExit: function() {},
Not before the first rendering though, then onInit is
called
myOwnMethod: function() {}
onAfterRendering });
Fired when the view has been rendered (the HTML is
injected into the document)
onExit
Fired when the controller is destroyed
Can be used to free resources
Example XML View
<core:View xmlns:core="sap.ui.core"
xmlns="sap.ui.commons"
controller="sap.hcm.Address">
<Panel>
<Button press="sayHello"
text="Say Hello" />
</Panel>
</core:View>
Example JSON View
{
"Type":"sap.ui.core.mvc.JsonView",
"controllerName":"sap.hcm.Address",
"content": [{
"Type": "sap.ui.commons.Button",
"id": "MyButton",
"text": "Say Hello",
"press": "sayHello"
}]
}
Example JS View
sap.ui.jsview("sap.hcm.Address", {
getControllerName: function() {
return "sap.hcm.Address";
},
createContent: function(oController) {
var oButton = new sap.ui.commons.Button({ text: "Say Hello" });
/* closure, so controller is known in event handler */
oButton.attachPress(function() {
oController.sayHello();
})
return oButton;
}
});
Databinding
UI5 Data Binding
In UI5, data binding is used to bind UI5 controls to a data source that holds the application
data, so that the controls are updated automatically whenever the application data is
changed.
With two-way-binding the application data is updated whenever the value of a bound control
changes, e.g. through user input.
Data binding supports binding of simple controls like TextField and list type controls like
DataTable and DropdownBox.
See the complete documentation on how data binding works and how to implement it in
an application.
Data Binding Model Implementations
UI5 data binding supports three different model implementations.
JSON model XML model OData model
supports supports XML data supports OData
data in a compliant data
JavaScript supports two way
Object binding creates OData
Notation requests and handles
format OData responses
supports includes the open
two way source library dataJS
binding to handle OData
requests and data
Additionally there is support to use the ResourceModel with UI5 data binding.
Creating a Model Instance
To use data binding in a SAPUI5 applications you will need to instantiate the
appropiate model first. The constructor takes the URL of the model data or
the data itself as the first parameter.
JSON-Model:
var oModel = new sap.ui.model.json.JSONModel(dataUrl);
XML-Model:
var oModel = new sap.ui.model.xml.XMLModel(dataUrl);
OData-Model:
var oModel = new sap.ui.model.odata.ODataModel(dataUrl [, useJSON,
user, pass] );
Assigning the Model
After the model has been created you can assign the model to the Core or specific controls with the
setModel method.
The relevant model for a control is the one which is nearest to it on the path up to the root (UI area).If
there is no model in the root path found the one attached to the core becomes the relevant model
//global model
sap.ui.getCore().setModel(oModel);
With the ResourceModel, there is a wrapper for resource bundles that exposes the localized texts as a
model for data binding.
A ResourceModel can be instantiated with a bundleName or a bundleUrl which points to a resource
bundle. When using the bundle name the file must have the .properties suffix.
Example:
var oModel = new sap.ui.model.resource.ResourceModel({
bundleName: "myBundle", // will use the file myBundle_en.properties
locale: "en"
});
var oControl = new sap.ui.commons.Button({
id : "myButton",
text : "{i18n>MY_BUTTON_TEXT}"
});
// attach the resource model with the symbolic name "i18n"
sap.ui.getCore().setModel(oModel, "i18n");
About Binding Paths
Aggregation binding is used to bind a collection of values, like binding multiple rows to a table. To use
aggregation you will have to use a control that acts as a template.
var oItemTemplate = new sap.ui.core.ListItem({ text: "{aggrProperty}" });
var oComboBox = new sap.ui.commons.ComboBox({
items: { path:"/modelAggregation", template: oItemTemplate }
});
The aggregation binding can also be defined using the bindAggregation method of a control.
oComboBox.bindAggregation( "items", "/modelAggregation", oItemTemplate );
Example for Simple Control
//create the template control that will be repeated and will display the data
var oRowTemplate = new sap.ui.commons.Message("rowTemplate", {
text: "{description}",
type: "{level}"
});
Instead of just using the token name of a model property you can also use the
extended data binding syntax. This enables you to use formatters and the type
system for property binding and templates, filter and sorters for aggregation
binding.
To use the extended syntax you supply an object literal for the bound
property/aggregation.
Extended property binding: Extended aggregation binding:
{ {
path: path: "/path/to/aggregation",
"/path/to/model/property", template: oItemTemplate,
formatter: fnCallback, sorter: oSorter,
type: oType filters:
} [oFilter1,oFilter2,oFilter3]
}
Formatters
For properties you can supply a formatter function which will be called with the value of the model
property. The return value of the formatter function is used as the value of the bound control.
var oTextField = new sap.ui.commons.TextField({
value: {
path: "/path/to/property"
formatter: upperCase
}
});
function upperCase(sVal) {
return sVal.toUpperCase();
}
Sorter and Filter
When using aggregation binding, you can provide initial sorting and filtering.
var oSorter = new sap.ui.model.Sorter("name", true); // sort descending
var oFilter1 = new sap.ui.model.Filter("name",
sap.ui.model.FilterOperator.StartsWith, "M");
var oFilter2 = new sap.ui.model.Filter("name",
sap.ui.model.FilterOperator.Contains, "Doe");
var oFilter3 = new sap.ui.model.Filter("name",
sap.ui.model.FilterOperator.BT, "A","G"); // name between A and G
var oComboBox = new sap.ui.commons.ComboBox({
items: {
path: "/path/to/aggregation",
template: oItemTemplate,
sorter: oSorter,
filters: [oFilter1,oFilter2,oFilter3]
}
});
Type System
since 1.4.0
Data binding supports the definition of types which can be handed over when binding properties. Bound
properties with a defined type will automatically be formatted when displayed in the UI, input values in
UI controls are parsed and converted back to the defined type in the model.
For each Type you can define the following parameters in the constructor:
format options: Format options define how a value is formatted and displayed in the UI.
constraints (optional): Constraints define how an input value entered in the UI should look like. When
parsing the value will be validated against these constraints.
Type System Example
To catch invalid user input, you can register the following handlers to the SAPUI5 Core.
attachFormatError
attachParseError
attachValidationError
attachValidationSuccess
Example:
sap.ui.getCore().attachValidationError(function(oEvent) {
var oElement = oEvent.getParameter("element");
if (oElement.setValueState) {
oElement.setValueState(sap.ui.core.ValueState.Warning);
}
});
OData - Open Data Protocol
In some cases one needs to adjust parts of the theme using CSS
One can add <style> or <link> tags in the HTML <head> tag to include new styles
These styles are always processed after the UI5 CSS from the themes
As the last processed CSS wins in case of multiple same CSS rules, the custom CSS always overwrites
the standard UI5 CSS
The parts to be overwritten can be determined using tools like Firebug
It is also important to know that the id given to a control is also the id that the topmost HTML element
of this control gets. Thus, this id can be used as a reference in CSS
@-sap-parameters {
sapUiTextColor: #000000; /* text color is 'black' */
[...]
}
button {
color: -sap-par(sapUiTextColor); /* current text color */
[...]
}
UI5 Theming: CSS Parameters II
jQuery.sap.require("sap.ui.core.theming.Parameters");
var myColor = sap.ui.core.theming.Parameters.get("sapUiSemanticCriticalColor"))
Control Style and Themes: Supported themes
Themes shipped with UI5:
Gold Reflection
Blue Crystal
Ux Target Design:
specification
See all these themes
showcased in an example
Extending UI5
Inclusion of external JS libraries
If UI5 doesnt provide controls or behaviours that other JavaScript libraries offer, these libraries can be
included into the UI5 based pages
Some JS libraries are already included in UI5 and used by UI5 runtime and can be used without additional loading
jQuery, jQuery UI
dataJS, a Microsoft driven oData library which handles requests and takes care of creating and handling requests in JSON
and XML format
Other libraries need to be loaded separately, currently other libraries of interest are
Flot, a <canvas> based jQuery plug in for displaying data sets using plot graphics
Protovis, an SVG based graphical library to visualize data sets
Inclusion of external JS libraries
One can place the script tags for the library just behind the UI5 bootstrap script tag I n
the HTML <head> tag
the library can be download and made part of the application project
For pie chart support you need also the flot.pie.js plugin
Most libraries require an // create the HTML control which will be a placeholder
staring point or a var oHTML = new sap.ui.core.HTML({
placeholder id: "pieContainer,
content: "<div id=pieContainer'
There are two approaches to develop UI5 Controls, either with tool support, or native in the
editor. This section deals with creating custom controls with the extend method.
Since an IDE is not needed to create new controls with the extend method these controls have
been named "Notepad Controls".
Technically, this functionality is not restricted to Controls. Arbitrary objects derived from
sap.ui.base.Object can be created or extended.
The extend Method
The extend() method is available on all Controls (and the base classes) and is used to define
a new subclass.
Creating a new control:
sap.ui.core.Control.extend(sName, oDefinition);
The definition object for a custom control may contain metadata, public and private methods, event
handler and the renderer.
sap.ui.core.Control.extend("TestControl", {
metadata: { // defines the properties, aggregations, events
properties: {},
events: {},
aggregations: {}
},
publicMethod: function() {}, // all methods are public
_privateMethod: function() {}, // private methods are prefixed with _
init: function() {} // called when control is instantiated
onclick: function(e) {}, // event handler
renderer: function(rm, oControl) {} // creates the html for the control
});
Control Metadata
The metadata in the control definition consists of objects for the control
properties, events and aggregations.
Properties Example
type: The data type of the Control properties: {
property. title: "string",
btnTxt: { defaultValue:
string for a string property (default)
"Search" },
int or float for number properties showLogoutButton: {
int[] for an array of integers, string[] for type: "boolean",
an array of strings, etc. defaultValue: true
},
sap.ui.core.CSSSize for a custom-defined
type width: {
type: "sap.ui.core.CSSSize",
defaultValue: The default value of defaultValue: "50px"
the property. (undefined if not set) }
}
Control Metadata
continued
Events Aggregations
Events are specified by the event Aggregations and associations are
name only. In many cases there is defined by their name, along with an
nothing to configure about them, object that can have a type, a
so just give an empty object. multiple flag and a singularName.
Controls can enable events to be aggregations: {
interrupted by the application, acceptButton:
using the enablePreventDefault "sap.ui.commons.Button",
flag. worksetItems: {
type:
events: {
logout: {}, "sap.ui.ux3.NavigationItem",
close: { multiple: true,
enablePreventDefault : true singularName : "worksetItem"
} }
} }
Control Methods
After the metadata is defined, you can add any method implementations to your new Control. The
method names can be chosen freely, some method names must be avoided though:
Names of methods that are provided by a super class.
Methods starting with set, get, insert, add, remove or indexOf may collide with setters/getters for properties or
aggregations you defined.
Methods starting with attach, detach or fire may collide with methods created for events.
There are some method names you may use but which have a special meaning:
on...: Methods starting with "on" are event handlers that are automatically bound to browser events.
init: Is the name of the initialization function called right after Control instantiation.
renderer: The name of the function that creates the HTML for the control.
Control Methods
continued
Bar Events
sap.viz.ui5.Bar selectData ( oControlEvent )
Line
sap.viz.ui5.Line CVOM API Reference
Pie
sap.viz.ui5.Pie
Combination
sap.viz.ui5.Combination
Exercise CVOM Pie Chart
Extend the UI5 bootstrap to the CVOM Chart library
sap.service.visualization
oDataTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({ text: "Country"}),
template : oControl
}));
oDataTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({ text: "profit"}),
template : oControl
}));
Solution to Exercise
oDataTable.addColumn(new sap.ui.table.Column({
label : new sap.ui.commons.Label({ text: "revenue"}),
template : oControl
}));
oDataTable.setModel(oJSONModel);
oDataTable.bindRows("chartData");
oDataset.setDataTable(oDataTable);
sap-ui-core.js
loads library.js file for each used library.
loads control behaviour and rendering files when
control is instantiated.
only needed code is loaded
many requests
sap-ui-core.js: 99 requests, 1,06 MB, 831ms
sap-ui-core-all.js
loads the complete library, including the code for all
controls
less requests
bigger filesize