As your customer needs the full overview to make decisions quickly, you will give them an option to narrow down the list of sensors based on the current sensor temperature.
For this, we enhance our sap.m.IconTabBar
control.
-
Open
sensormanager/webapp/view/Sensors.view.xml
. -
Add
sap.m.IconTabFilter
elements to theitems
aggregation of thesap.m.IconTabBar
control. They will be visible as icons above the bar, so that the user can click them to filter the list.
sensormanager/webapp/view/Sensors.view.xml
<IconTabBar id="iconTabBar" class="sapUiResponsiveContentPadding">
<items>
<IconTabFilter showAll="true" text="{i18n>msgFilterAll}" key="All"/>
<IconTabSeparator/>
<IconTabFilter icon="sap-icon://fridge" iconColor="Default" text="{i18n>msgFilterCold}" key="Cold"/>
<IconTabFilter icon="sap-icon://blur" iconColor="Critical" text="{i18n>msgFilterWarm}" key="Warm"/>
<IconTabFilter icon="sap-icon://warning" iconColor="Negative" text="{i18n>msgFilterHot}" key="Hot"/>
</items>
<content>
...
- Let's see if your UI5 application now displays the newly introduced
sap.m.IconTabFilter
elements! Switch to the browser tab with the opened application preview and reload the page.
In the previous section you've added all necessary controls. Next, you'll implement the filtering logic.
-
Open
sensormanager/webapp/controller/Sensors.controller.js
. -
Add the module
sap/ui/model/Filter
as a dependency toSensors.controller.js
.
sensormanager/webapp/controller/Sensors.controller.js
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/IconColor",
"sap/m/MessageToast",
"sap/ui/model/Filter"
], function (Controller, IconColor, MessageToast, Filter) {
"use strict";
- Implement the
onSensorSelect
function for filtering the sensor list items by checking theirstatus
property. We'll also make use of the previously defined threshold and use some filter settings to narrow down the result.LT
for example means "less than".
sensormanager/webapp/controller/Sensors.controller.js
onSensorSelect: function (oEvent) {
this._aCustomerFilters = [];
this._aStatusFilters = [];
var oBinding = this.getView().byId("sensorsList").getBinding("items"),
sKey = oEvent.getParameter("key");
if (sKey === "Cold") {
this._aStatusFilters = [new Filter("temperature", "LT", this.oThreshold.Warm, false)];
} else if (sKey === "Warm") {
this._aStatusFilters = [new Filter("temperature", "BT", this.oThreshold.Warm, this.oThreshold.Hot, false)];
} else if (sKey === "Hot") {
this._aStatusFilters = [new Filter("temperature", "GT", this.oThreshold.Hot, false)];
} else {
this._aStatusFilters = [];
}
oBinding.filter(this._aStatusFilters);
}
The filtering logic has been written. Next, you need to assign the filtering function to the select
event of the sap.m.IconTabBar
.
-
Open
sensormanager/webapp/view/Sensors.view.xml
. -
Bind the
onSensorSelect
function to theselect
event of theIconTabBar
. Whenever one of thesap.m.IconTabFilter
elements is clicked, this function will be called.
sensormanager/webapp/view/Sensors.view.xml
<IconTabBar id="iconTabBar" select=".onSensorSelect" class="sapUiResponsiveContentPadding">
- Let's see if your UI5 application is now able to filter the sensor data correctly. Switch to the browser tab with the opened application preview and reload the page. Click the Too Hot icon. Only sensors with too high a temperature are displayed.
Your customer wishes to display the total number of sensors as well. For this, you can introduce the count
property of sap.m.IconTabFilter
.
-
Open
sensormanager/webapp/view/Sensors.view.xml
. -
Make use of an expression binding by adding the
count
property and the expression binding{=${sensorModel>/sensors}.length}
.
sensormanager/webapp/view/Sensors.view.xml
<IconTabFilter
showAll="true"
text="{i18n>msgFilterAll}"
key="All"
count="{=${sensorModel>/sensors}.length}"/>
- Let's see if your UI5 application can display the total number of sensors correctly. Switch to the browser tab with the opened application preview and reload the page. Do you see 100? Yeah!
Hooray! You've successfully completed Exercise 6 - Filtering with the IconTabBar.
Continue to Exercise 7 - Fragment containing a SelectDialog.
- Model Filter in UI5: https://ui5.sap.com/#/topic/5295470d7eee46c1898ee46c1b9ad763
- Expression Binding: https://ui5.sap.com/#/topic/daf6852a04b44d118963968a1239d2c0