I am working on a SAPUI5 application using CAP Business Application Studio (BAS), and I have a LaunchPad view with a tile that navigates to the PlantsList view. I want to display the count of plants in the LaunchPad view using an OData service.
How can I correctly consume the OData service in the LaunchPad controller to perform the $count operation on the "Plants" entity?
Are there any specific considerations or configurations in SAPUI5 or CAP BAS that I need to be aware of when working with OData services and performing count operations?
LaunchPad.controller
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History",
"sap/ui/model/json/JSONModel"
], function (Controller, History, JSONModel) {
"use strict";
return Controller.extend("farm.Farm.controller.LaunchPad", {
onInit: function() {
this._bindPlantCount();
},
_bindPlantCount: function() {
const oModel = this.getOwnerComponent().getModel();
oModel.read("/Plants/$count", {
success: function (oData) {
const iPlantCount = parseInt(oData, 10);
const oViewModel = new JSONModel({
plantCount: iPlantCount
});
this.getView().setModel(oViewModel, "view");
}.bind(this),
error: function () {
// Handle error
}
});
},
onPress: function () {
this.getOwnerComponent().getRouter().navTo("PlantList");
},
onNavBack: function () {
var sPreviousHash = History.getInstance().getPreviousHash();
if (sPreviousHash !== undefined) {
history.go(-1);
} else {
this.getOwnerComponent().getRouter().navTo("Login", {}, true);
}
}
});
});
LaunchPad.view
<mvc:View controllerName="farm.Farm.controller.LaunchPad" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core">
<core:Fragment fragmentName="farm.Farm.view.Header" type="XML"/>
<GenericTile class="sapUiTinyMarginBegin sapUiTinyMarginTop tileLayout" header="Farmbot" subheader="Plants List" press="onPress">
<TileContent unit="" footer="">
<NumericContent value="{plantsModel>/plantsCount}" withMargin="false"/>
</TileContent>
</GenericTile>
</mvc:View>
Manifest.json
{
"_version": "1.12.0",
"sap.app": {
"id": "farm.Farm",
"type": "application",
"i18n": "i18n/i18n.properties",
"applicationVersion": {
"version": "1.0.0"
},
"title": "{{appTitle}}",
"description": "{{appDescription}}",
"dataSources": {
"mainService" : {
"uri": "odata/v4/catalog/",
"type": "OData",
"settings": {
"annotations": [],
"localUri": "localService/metadata.xml",
"odataVersion":"4.0"
}
}
},
"sourceTemplate": {
"id": "ui5template.basicSAPUI5ApplicationProject",
"version": "1.40.12"
}
},
"sap.ui": {
"technology": "UI5",
"icons": {
"icon": "",
"favIcon": "",
"phone": "",
"phone@2": "",
"tablet": "",
"tablet@2": ""
},
"deviceTypes": {
"desktop": true,
"tablet": true,
"phone": true
}
},
"sap.ui5": {
"flexEnabled": false,
"rootView": {
"viewName": "farm.Farm.view.App",
"type": "XML",
"async": true,
"id": "App"
},
"dependencies": {
"minUI5Version": "1.65.6",
"libs": {
"sap.ui.layout": {},
"sap.ui.core": {},
"sap.m": {}
}
},
"contentDensities": {
"compact": true,
"cozy": true
},
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "farm.Farm.i18n.i18n"
}
},
"": {
"type": "sap.ui.model.odata.v4.ODataModel",
"dataSource": "mainService",
"preload": true,
"settings": {
"synchronizationMode": "None",
"operationMode": "Server",
"autoExpandSelect": true,
"earlyRequests": true,
"groupId": "$auto"
}
}
},
"resources": {
"css": [{
"uri": "css/style.css"
}]
},
"routing": {
"config": {
"routerClass": "sap.m.routing.Router",
"viewType": "XML",
"async": true,
"viewPath": "farm.Farm.view",
"controlAggregation": "pages",
"controlId": "app",
"clearControlAggregation": false
},
"routes": [{
"pattern": "/",
"name": "Login",
"target": ["login"]
}, {
"pattern": "launchpad",
"name": "launchpad",
"target": ["launchpad"]
},
{
"pattern": "PlantList",
"name": "PlantList",
"target": ["plantList"]
},
{
"pattern": "Details/{Plants/ID}",
"name": "Details",
"target": ["details"]
}],
"targets": {
"login": {
"viewName": "Login",
"viewLevel": 1
},
"launchpad": {
"viewName": "LaunchPad",
"viewLevel": 2
},
"plantList": {
"viewName": "PlantList",
"viewLevel": 3
},
"details": {
"viewName": "Details",
"viewLevel": 4
}
}
}
}
}
I attempted to use the read method of the OData model to perform the $count operation on the "Plants" entity. I expected to retrieve the count of plants and bind it to a JSONModel for display in the LaunchPad view. However, the count is not being retrieved as expected, and I failed to get the Model
In your launchpad.controller you are setting your oViewModel to the the view as name model using the 'view' alias.
and later in the LaunchPad.view you are using plantsModel as json Model instead of 'view'.
Ensure consistency by using the same model alias throughout. If you intended to use 'view' as the model alias, make sure to update the LaunchPad.view to reflect this change: