cancel
Showing results for 
Search instead for 
Did you mean: 

How to sum Time Fields in SAP UI5

Floatjitsu
Explorer
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Floatjitsu
Explorer
0 Kudos

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));
}

Answers (2)

Answers (2)

jorge_cabanas
Participant
0 Kudos

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)

former_member322772
Active Participant
0 Kudos

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.

Floatjitsu
Explorer
0 Kudos

Hey Luke,

is it possible to add a formatter function without having the "parts" segment of it? How would I write this and especially where?

Best regards