You achieved a lot in the previous exercises. Now it's time to dress up your UI5 application with some visual effects!
To give the customer the best possible overview, add some color to your application. Introduce a new layout and structure for the item, and also show an sap.ui.core.Icon
there.
- Open
Sensors.view.xml
and add the xml namespacexmlns:core="sap.ui.core"
to the view to have thesap.ui.core.Icon
available.
SensorManager/webapp/view/Sensors.view.xml
<mvc:View
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns:grid="sap.ui.layout.cssgrid"
xmlns:f="sap.f"
xmlns="sap.m"
displayBlock="true">
- Add a temperature icon as well as layouting to the
sap.m.CustomListItem
control.sapUiSmallMarginTop
andsapUiSmallMarginEnd
are predefined device agnostic css classes, which add spacing to controls.sap.m.HBox
andsap.m.VBox
are helpers for layouting your application.
SensorManager/webapp/view/Sensors.view.xml
<CustomListItem type="Active">
<layoutData>
<FlexItemData growFactor="1" shrinkFactor="0"/>
</layoutData>
<HBox justifyContent="SpaceBetween">
<VBox justifyContent="SpaceBetween" class="sapUiSmallMarginTop sapUiSmallMarginBegin">
<Title text="{sensorModel>location}"/>
<Label text="{i18n>distanceLabel}:"/>
</VBox>
<core:Icon src="sap-icon://temperature" size="2.5rem" class="sapUiSmallMarginTop sapUiSmallMarginEnd"/>
</HBox>
<HBox justifyContent="SpaceBetween" class="sapUiTinyMarginTop sapUiSmallMarginBottom sapUiSmallMarginBeginEnd">
<ObjectNumber number="{sensorModel>distance}" unit="{i18n>distanceUnit}"/>
</HBox>
</CustomListItem>
In this exercise you'll enhance your application with some additional controller coding.
-
Go to folder
SensorManager/webapp/controller/
. -
All functions defined in the Controller can be used in your View. This gives you more flexibility to implement specific functionality to improve the visualization in your View. Copy and paste the following code into
Sensors.controller.js
:
SensorManager/webapp/controller/Sensors.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller"
],
function (Controller) {
"use strict";
return Controller.extend("keepcool.SensorManager.controller.Sensors", {
onInit: function () {
}
});
});
- Add the modules
sap/ui/core/IconColor
andsap/m/MessageToast
as dependencies to theSensors.controller.js
. You'll use them later on during this exercise.
SensorManager/webapp/controller/Sensors.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/IconColor",
"sap/m/MessageToast"
], function (Controller, IconColor, MessageToast) {
"use strict";
- Your next goal is to show an
sap.m.MessageToast
when your sensor data is loaded. Paste the following content into theonInit
function ofSensors.controller.js
. This also adds a functiongetSensorModel
to retrieve the sensor model.
SensorManager/webapp/controller/Sensors.controller.js
onInit: function() {
this.getSensorModel().dataLoaded().then(function() {
MessageToast.show(this.getOwnerComponent().getModel("i18n").getResourceBundle().getText("msgSensorDataLoaded"), { closeOnBrowserNavigation: false });
}.bind(this));
},
getSensorModel: function(){
return this.getOwnerComponent().getModel("sensorModel");
}
Now you need to tell your view which controller is associated with it.
-
Open
SensorManager/webapp/view/Sensors.view.xml
. -
Add the
controllerName
attribute to the view and enter the controller namekeepcool.SensorManager.controller.Sensors
. The Controller's functions can then be used in your View. UI5 maps this path to theSensorManager/webapp/controller/Sensors.controller.js
file.
SensorManager/webapp/view/Sensors.view.xml
<mvc:View
controllerName="keepcool.SensorManager.controller.Sensors"
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns:grid="sap.ui.layout.cssgrid"
xmlns:f="sap.f"
xmlns="sap.m"
displayBlock="true">
- Let's see if your UI5 application is able to show the
sap.m.MessageToast
! Switch to the browser tab with the opened application preview and reload the page. Thesap.m.MessageToast
should be displayed to confirm that your sensor data has been loaded successfully.
Your next goal is to bring some color to the user interface. You'd like to display the icon in a suitable color which is based on the actual temperature of the sensor. To do this, you can use the formatter concept of UI5.
-
Open
SensorManager/webapp/controller/Sensors.controller.js
. -
Add the new function
formatIconColor
.
SensorManager/webapp/controller/Sensors.controller.js
formatIconColor: function(iTemperature) {
var oThreshold = this.getSensorModel().getProperty("/threshold");
if (!oThreshold) {
return IconColor.Neutral;
} else if (iTemperature < oThreshold.warm) {
return IconColor.Default;
} else if (iTemperature >= oThreshold.warm && iTemperature < oThreshold.hot) {
return IconColor.Critical;
} else {
return IconColor.Negative;
}
}
You're almost done. The last piece is adding the newly created formatter function to the binding of your icon.
-
Open
SensorManager/webapp/view/Sensors.view.xml
. -
Add the
color
property to thesap.ui.core.Icon
definition, bind thecolor
property to the pathsensors>temperature/value
, and assign the formatter function to the binding.
SensorManager/webapp/view/Sensors.view.xml
<core:Icon src="sap-icon://temperature" size="2.5rem"
color="{path: 'sensorModel>temperature/value', formatter:'.formatIconColor'}"
class="sapUiSmallMarginTop sapUiSmallMarginEnd"/>
- Let's see if your UI5 application can now color icons depending on the sensor data! Switch to the browser tab with the opened application preview and reload the page. The sensor icons should be displayed either in blue (default), yellow (critical) or red (negative).
Congratulations, you completed the Exercise 5 - Improve Visualization exercise!
Continue to Exercise 6 - Filtering with the IconTabBar.
- Model View Controller Concept: https://ui5.sap.com/#/topic/91f233476f4d1014b6dd926db0e91070
- Controller: https://ui5.sap.com/#/topic/121b8e6337d147af9819129e428f1f75
- Formatting, Parsing, and Validating Data: https://ui5.sap.com/#/topic/07e4b920f5734fd78fdaa236f26236d8