on 10-23-2018 4:20 PM
Hi gurus,
in my Application, I got a table like this:
Now I want to sum up all of these hours and wirte the result to the bottom of the table. The cells are Edm.Time datatype coming from the backend and got formatted via custom formatter function. In this state, they are a simple string.
Any suggestion on how to achieve this?
Best regards
Timur
Hey all,
I solved the problem via Function Import. I created a Function Import in Backend called "GetHourSum" which provides me a result array in which I have all times in milliseconds Here is the Frontend code:
calcSum: function(context) {
var datum = this.byId("datum").getText();
var user = this.byId("user").getText();
var promise = new Promise(function(resolve, reject){
context.getOwnerComponent().getModel().callFunction("/GetHourSum", {
method: "GET",
urlParameters: {User: user, Datum: datum},
success: function(oData){
var sSum = 0;
for(var i = 0; i < oData.results; i++){
sSum = oData.results[i].Stunden.ms;
}
resolve(oData.results);
},
error: function(oError){
reject(oError);
}
});
});
return promise;
},
I also have a function, which I call everytime the view opens. In this function I call calcSum() and call the custom formatter Function addTimes:
onInit : function () { //attach matching function to Router
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.getRoute("zeiten").attachMatched(this.onRouteFound, this);
},
//call this function everytime the view is opened
onRouteFound: function(oEvt) {
var oArgument = oEvt.getParameter("arguments");
var oTable = this.byId("zeiten");
var oThat = this;
var sum = this.byId("sum");
this.calcSum(oThat).then(function(result){
var x = oThat.addTimes(result);
sum.setText(x);
});
},
//formatter function
addTimes: function(aTimes) {
var timeFormat = sap.ui.core.format.DateFormat.getTimeInstance({pattern: "KK:mm"});
var TZOffsetMs = new Date(0).getTimezoneOffset()*60*1000;
var sSum = 0;
for(var i = 0; i < aTimes.length; i++){
sSum += aTimes[i].Stunden.ms;
}
return timeFormat.format(new Date(sSum + TZOffsetMs));
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you try to sum them in the format that backend provided you (Before custom format)?
And after the result is associated to the field Sum (Which will have the same custom format than the others rows)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try using a formatter function. Pass the times in and write a function to return the sum. You'll probably just need to parse the string as a float with JavaScript.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.