Objective: This blog post provides insights about the Total and Sub-Total of a Numerical Units in a Table using Northwind Service and how to implement in SAP Business Application Studio environment.
In this blog post, I’ll demo about Total and Sub-Total of a Numerical Units in a Table using Northwind Service.
Northwind Service: Northwind Service is an OData Sample service and consumed in SAP UI5 and Fiori Applications.
Responsive Table: The responsive table automatically adjusts its appearance and behaviour based on the available screen size, making it suitable for both desktop and mobile applications.
Please refer below link:
https://ui5.sap.com/#/api/sap.m.Table
Use Case: The Total and Sub-total Concept in a table is commonly used in scenarios where you need to display aggregated values for columns like Amount/Prices based on certain criteria like Quantity,Unit ect. For this case i came up with this solution.
In this scenario, we’ll assume a simple sales order table with columns like Product, Quantity, Unit and Price.
So, let’s get started,
In System- “Northwind” Destination.
Give Service Path of Northwind Service- “/V2/Northwind/Northwind.svc/”
Select: Object Collection- Products Entity Set.
Object Collection Key- ProductID Property.
Object ID- CategoryID Property.
Ater giving all the required details click on the Next Button.
<mvc:View controllerName="com.total.samtotal.controller.Worklist"
xmlns="sap.m"
xmlns:mvc="sap.ui.core.mvc"
xmlns:semantic="sap.m.semantic"
xmlns:core="sap.ui.core">
<semantic:SemanticPage class="sapUiSizeCompact" id="page" title="{i18n>worklistTitle}" showFooter="false" navButtonPress="onNavBack" showNavButton="false">
<semantic:content>
<Toolbar>
<ToolbarSpacer />
<Button icon="sap-icon://sum" press="SumList" type="Accept" tooltip="{i18n>Total}"></Button>
<Button id="subTotBtn" icon="sap-icon://add-process" press="openSubTotalFunc" type="Accept" tooltip="{i18n>SubTotal}"></Button>
<Button icon="sap-icon://refresh" press="onTableRefresh" type="Emphasized"></Button>
</Toolbar>
<Table id="tableSum" width="auto" items="{path: 'ProductModel>/',sorter: {path: 'SupplierID',descending: false}}" noDataText="{i18n>tableNoDataText}" growing="true" growingThreshold="100" growingScrollToLoad="true">
<columns>
<Column width="5%">
<Label text="ID" design="Bold"></Label>
</Column>
<Column >
<Label text="Product Name" design="Bold"></Label>
</Column>
<Column >
<Label text="CategoryID" design="Bold"></Label>
</Column>
<Column >
<Label text="Unit" design="Bold"></Label>
<footer>
<VBox items="{STableModel>/QTYUnit}">
<Text visible="true" text="{STableModel>}" />
</VBox>
</footer>
</Column>
<Column >
<Label text="Price" design="Bold"></Label>
<footer>
<VBox id="UnitPriceSumId" visible="false" items="{STableModel>/UnitPricetotalVal}">
<Text text="{path:'STableModel>'}" />
</VBox>
</footer>
</Column>
<Column >
<Label text="Discontinued" design="Bold"></Label>
</Column>
</columns>
<items>
<ColumnListItem >
<customData>
<core:CustomData key="mydata" value="{ProductModel>Flag}" writeToDom="true"></core:CustomData>
</customData>
<cells>
<Text text="{ProductModel>ProductID}"></Text>
<Text text="{ProductModel>ProductName}"></Text>
<Text text="{ProductModel>CategoryID}"></Text>
<Text text="{ProductModel>QuantityPerUnit}"></Text>
<Text text="{ProductModel>UnitPrice}"></Text>
<ObjectStatus text="{ProductModel>Discontinued}" state="{path: 'ProductModel>Discontinued',formatter: '.formatter.status'}" />
</cells>
</ColumnListItem>
</items>
</Table>
</semantic:content>
</semantic:SemanticPage>
</mvc:View>
sap.ui.define([
"./BaseController",
"sap/ui/model/json/JSONModel",
"../model/formatter",
"sap/ui/model/Filter",
"sap/ui/model/FilterOperator",
"sap/ui/model/Sorter"
], function (BaseController, JSONModel, formatter, Filter, FilterOperator, Sorter) {
"use strict";
return BaseController.extend("com.total.samtotal.controller.Worklist", {
formatter: formatter,
/* =========================================================== */
/* lifecycle methods */
/* =========================================================== */
/**
* Called when the worklist controller is instantiated.
* @public
*/
onInit: function () {
var oViewModel, that = this;
// Model used to manipulate control states
oViewModel = new JSONModel({
worklistTableTitle: this.getResourceBundle().getText("worklistTableTitle")
});
this.setModel(oViewModel, "worklistView");
that.onGetService();
},
onGetService: function () {
var that = this;
var TableModel = new JSONModel();
that.getView().setModel(TableModel, "STableModel");
//Read Call
var oDataHeader = this.getOwnerComponent().getModel();
oDataHeader.read("/Products", {
success: function (oData, res) {
var sArray = []
var sData = oData.results;
for (var i = 0; i < oData.results.length; i++) {
var oQtySplit = sData[i].QuantityPerUnit.split(/(\d+)/);
var oQty = oQtySplit[oQtySplit.length - 1].toUpperCase();
var sObj = {};
sObj.CategoryID = sData[i].CategoryID;
sObj.Discontinued = sData[i].Discontinued;
sObj.ProductID = sData[i].ProductID;
sObj.ProductName = sData[i].ProductName;
sObj.QuantityPerUnit = oQty;
sObj.ReorderLevel = sData[i].ReorderLevel;
sObj.SupplierID = sData[i].SupplierID;
sObj.UnitPrice = parseFloat(sData[i].UnitPrice).toFixed(2);
sObj.UnitsInStock = sData[i].UnitsInStock;
sObj.UnitsOnOrder = sData[i].UnitsOnOrder;
sArray.push(sObj);
}
var ProductModel = new JSONModel(sArray);
ProductModel.setSizeLimit(sArray.length);
that.getView().setModel(ProductModel, "ProductModel");
}
});
},
});
});
//**************************** Sum List Fields Dialog *****************************//
SumList: function () {
var that = this;
var oPricePath = "UnitPrice";
var oUnit = "QuantityPerUnit";
var oData = [];
var oTable = that.byId("tableSum");
var uniqueQuantityUnit = [];
oTable.getItems().filter(function (item) {
var oPath = item.getBindingContextPath();
var oContext = oTable.getModel("ProductModel").getProperty(oPath);
oData.push(oContext);
if (uniqueQuantityUnit.indexOf(oContext[oUnit]) === -1) {
uniqueQuantityUnit.push(oContext[oUnit]);
}
});
that.getView().getModel("STableModel").setProperty("/QTYUnit", uniqueQuantityUnit);
var oTotalArray = [];
for (var i = 0; i < uniqueQuantityUnit.length; i++) {
var sArray = that.onTotalUnitQtyData(uniqueQuantityUnit[i], oData, oPricePath, oUnit);
oTotalArray.push(sArray);
}
that.byId(oPricePath + "SumId").setVisible(true);
that.getView().getModel("STableModel").setProperty("/" + oPricePath + "totalVal", oTotalArray);
that.getView().getModel("STableModel").setProperty("/sTotalPath", oPricePath);
var sumMsg = "Sum Calculated";
sap.m.MessageToast.show(sumMsg);
},
onTotalUnitQtyData: function (unitQty, oData, oPricePath, unit) {
var oTotal = 0;
for (var i = 0; i < oData.length; i++) {
if (unitQty === oData[i][unit]) {
var stData = oData[i][oPricePath];
var oValue = parseFloat(stData);
oTotal += oValue;
}
}
var oMainTotal = parseFloat(oTotal).toFixed(2);
return oMainTotal;
},
//**************************** Sum Sub Total List Fields Dialog *****************************//
openSubTotalFunc: function () {
var that = this;
// CategoryID is field with which we do subtotal functionality in table
var fieldName = "CategoryID";
var oPricePath = "UnitPrice";
var oView = that.getView();
var oTable = oView.byId("tableSum");
var unique = [], oData = [];
var qty = 0;
oTable.getItems().filter(function (item) {
var oPath = item.getBindingContextPath();
var oContext = oTable.getModel("ProductModel").getProperty(oPath);
oData.push(oContext);
if (unique.indexOf(oContext[fieldName]) === -1) {
unique.push(oContext[fieldName]);
}
});
unique.forEach(function (uniqVal) {
var sObj = {};
for (var i = 0; i < oTable.getItems().length; i++) {
var sPath = oTable.getItems()[i].getBindingContextPath();
var sContext = oTable.getModel("ProductModel").getProperty(sPath);
if (uniqVal === sContext[fieldName]) {
qty = that.onTotalUnitQtyData(uniqVal, oData, oPricePath, fieldName);
sObj[oPricePath] = qty;
}
}
sObj[fieldName] = uniqVal;
if (sObj[fieldName] === uniqVal) {
sObj.Flag = "LightOrange";
}
oData.splice(oData.length, 0, sObj);
oTable.getModel("ProductModel").setData(oData);
oTable.getModel("ProductModel").setSizeLimit(oData.length);
oTable.getModel("ProductModel").refresh();
oTable.getBinding("items").sort(new Sorter(fieldName, false));
qty = 0;
});
}
tr[data-mydata="LightOrange"]{
background: #FFD898 !important;
}
tr[data-mydata="None"]{
background: #f7f7f7 !important;
}
/*** Event handler for refresh event/ Refreshing Table */
onTableRefresh: function () {
var that = this;
that.onGetService();
},
Conclusion:
We had seen how to do Total and Sub-Total of Units with Values in a Table using Northwind Service.
NOTE: Displaying Table data using Northwind Service.
Regards,
Swathi Rani.