This topic is currently only applicable to SAP Fiori elements for OData V2.
Use app extensions with caution and only if you cannot produce the required
behavior by other means, such as manifest settings or annotations. To correctly
integrate your app extension coding with SAP Fiori elements, use
only the extensionAPI
of SAP Fiori elements. For
more information, see Using the extensionAPI.
After you've created an app extension, its display (for example, control
placement and layout) and system behavior (for example, model and binding usage,
busy handling) lies within the application's responsibility. SAP Fiori elements
provides support only for the official extensionAPI
functions.
Don't access or manipulate controls, properties, models, or other internal
objects created by the SAP Fiori elements
framework.
In this example, we assume that you want to add a Price field with two filter options to the smart filter bar of the Manage Products app's list report. To do so, you have to complete the following steps:
i18n
filemanifest.json
fileYou can only add new fields to the smart filter bar, not change existing ones. If you want to change existing fields, you must remove them first completely from the annotations, then add them again as new fields.
In the SAP Business Application Studio or Visual Studio Code, open the folder structure of the Manage Products project and then proceed as follows:
webapp
folder, create a new subfolder called
ext
.ext
, create a new subfolder called
fragment
.fragment
folder, create file
Custom.Filter.fragment.xml
.controller
folder, create file
Custom.Filter.controller.js
.ControlConfiguration
to the smart filter bar.
You can see the options for the ComboBox
in the following
example:
<core:FragmentDefinition xmlns="sap.m" xmlns:smartfilterbar="sap.ui.comp.smartfilterbar" xmlns:core="sap.ui.core"> <!-- Price Filter--> <smartfilterbar:ControlConfiguration key="CustomPriceFilter" index="3" label="{i18n|sap.suite.ui.generic.template.ListReport|SEPMRA_C_PD_Product>xfld.Price}" visibleInAdvancedArea="true" groupId="_BASIC"> <smartfilterbar:customControl> <ComboBox id="CustomPriceFilter-combobox"> <core:Item id="CustomPriceFilterItem0" key="0" text="{i18n|sap.suite.ui.generic.template.ListReport|SEPMRA_C_PD_Product>xtit.Price_0-100}"/> <core:Item id="CustomPriceFilterItem3" key="1" text="{i18n|sap.suite.ui.generic.template.ListReport|SEPMRA_C_PD_Product>xtit.Price_GE100}"/> </ComboBox> </smartfilterbar:customControl> </smartfilterbar:ControlConfiguration> </core:FragmentDefinition>
You can use the index property to define the position of the
filterable field. For more information, see also Smart Filter Bar and
sap.ui.comp.smartfilterbar.ControlConfiguration
.
Note that if a user changes a filter field in
the filter bar, the table shows an overlay to indicate that the state of the
filter bar differs from the date currently being displayed. If the control
used in your app does not trigger a change event, you have to set this up
using .fireChange()
so that the filter bar will recognize
the change and display the overlay.
sap.ui.define([], function () { return { onBeforeRebindTableExtension: function(oEvent) { var oBindingParams = oEvent.getParameter("bindingParams"); oBindingParams.parameters = oBindingParams.parameters || {}; var oSmartTable = oEvent.getSource(); var oSmartFilterBar = this.byId(oSmartTable.getSmartFilterId()); var vCategory; if (oSmartFilterBar instanceof sap.ui.comp.smartfilterbar.SmartFilterBar) { //Custom price filter var oCustomControl = oSmartFilterBar.getControlByKey("CustomPriceFilter"); if (oCustomControl instanceof sap.m.ComboBox) { vCategory = oCustomControl.getSelectedKey(); switch (vCategory) { case "0": oBindingParams.filters.push(new sap.ui.model.Filter("Price", "LE", "100")); break; case "1": oBindingParams.filters.push(new sap.ui.model.Filter("Price", "GT", "100")); break; default: break; } } } } } });
To make the field name and the filter options translatable, add the texts to the
i18n
file as follows:
#XFLD: Custom filter breakout label xfld.Price=Price #XTIT: Price range 0-100 xtit.Price_0-100=Price between 0-100 #XTIT: Price range Over 100 xtit.Price_GE100=Price: Over 100
manifest.json
fileTo integrate the logic as an extension, define a view and controller extension to
load the files you created in Step 1 (Custom.Filter.fragment.xml
and Custom.Filter.controller.js
).
The logic is added to the ListReport
section of the Manage Products
app.
"extends": { "extensions": { "sap.ui.controllerExtensions": { "sap.suite.ui.generic.template.ListReport.view.ListReport": { "controllerName": "my_app.ext.controller.ListReportExtension" } }, "sap.ui.viewExtensions": { "sap.suite.ui.generic.template.ListReport.view.ListReport": { "SmartFilterBarControlConfigurationExtension|SEPMRA_C_PD_Product": { "className": "sap.ui.core.Fragment", "fragmentName": "ManageProducts.ext.fragment.CustomFilter", "type": "XML" } } } } },
The list report of the Manage Products app displays the new Price field with filter options.