In this exercise you'll add a local data service to the application and display the data on the enhanced UI5 view.
To simulate a source providing sensor data, you'll now add some sensor data to your application.
-
Rightclick on the
sensormanager/webapp/
folder and select New Folder. -
Enter localService as the folder name.
-
Right-click on the
localService
folder. -
Copy and paste the content of sensors.json into the newly created file.
After adding the sensor data to your application, you'll need to configure the data service which provides the sensor data.
-
Click on
manifest.json
link in the Application Info page. If you had closed the Application Info page, you can reopen it by using commandFiori: Open Application Info
from command palette. You can also find themanifest.json
file located undersensormanager/webapp
. -
Go to the section
sap.app
. Here, replace thedataSources
section as follwowing:
sensormanager/webapp/manifest.json
"sap.app": {
...
"dataSources": {
"sensorSource": {
"type": "JSON",
"uri": "./localService/sensors.json"
}
}
}
The sensorSource
, points to the sensor data which has just beed added to the localService
folder.
- Go to the section
sap.ui5
. Here, replace themodels
section as following:
sensormanager/webapp/manifest.json
"sap.ui5": {
...
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "keepcool.sensormanager.i18n.i18n"
}
},
"sensorModel": {
"type": "sap.ui.model.json.JSONModel",
"dataSource": "sensorSource"
}
},
The JSONModel
with name sensorModel
is defined here which will serve as the centrail datapoint in the application.
Note that this model is referencing to the data source that has been created prior to the model.
Since this workshop does not use flexibility services, disable the flex enablement as following:
"sap.ui5": {
"flexEnabled": false,
...
After configuring the data service, it's now time to enrich your Sensors.view.xml
with some fancy UI5 controls!
-
Open the
Sensors.view.xml
located undersensormanager/webapp/view
. -
Add
sap.f
andsap.ui.layout.cssgrid
to the xml namespace declarations to make sure that the required resources are available in your view.
sensormanager/webapp/view/Sensors.view.xml
<mvc:View
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:grid="sap.ui.layout.cssgrid"
xmlns:f="sap.f"
displayBlock="true">
- Add
sap.f.GridList
to thecontent
aggregation of the IconTabBar. An aggregation is a relation between two UI elements. It is used to define the parent-child relationship within the tree structure. The child end of the relation may have cardinality 0..1 or 0..*. The elements' API offers convenient and consistent methods to deal with aggregations (e.g. to get, set, or remove child elements). Examples are table rows and cells, or the content of a table cell.
sensormanager/webapp/view/Sensors.view.xml
<f:GridList id="sensorsList">
<f:customLayout>
<grid:GridBoxLayout/>
</f:customLayout>
<f:items>
<!-- item template will be added here! -->
</f:items>
<f:noData>
<IllustratedMessage enableVerticalResponsiveness="true" illustrationType="sapIllus-EmptyList"/>
</f:noData>
</f:GridList>
After adding the sap.f.GridList
control, you'll need to connect the control to the sensor data. For this, UI5 provides a mechanism called Data Binding.
-
Open the
Sensors.view.xml
located undersensormanager/webapp/view
. -
Bind the
items
aggregation of thesap.f.GridList
to the pathsensorModel>/sensors
. Here,sensorModel
is the name of your recently defined data model, and/sensors
points to a property inside it. As this is an array with several entries, you'd probably like to define sorting and grouping as well. In thesorter
you can configure this by using the properties available:
sensormanager/webapp/view/Sensors.view.xml
<f:GridList
id="sensorsList"
items="{path: 'sensorModel>/sensors', sorter: {path:'customer', group:true, descending: false}}"
noDataText="No sensors">
- The list items are defined once as a template, which is then repeated multiple times to represent each entry of the sensors array. We also add some location details to our
sap.m.CustomListItem
. Here,location
references the location property of each of the displayed sensor items. Replace the comment<!-- item template will be added here!>
with the following template:
sensormanager/webapp/view/Sensors.view.xml
<CustomListItem>
<Title text="{sensorModel>location}"/>
</CustomListItem>
- Let's see if our UI5 application displays the correct sensor data. Switch to the browser tab with the opened application preview and reload the page.
Congratulations! You've completed successully Exercise 3 - Show Sensor Content.
Continue to Exercise 4 - Introduce Localization.