cancel
Showing results for 
Search instead for 
Did you mean: 

How to display the count from oData Service in SAPUI5

Jayakrishnan
Active Participant
0 Kudos

Hi ,

   I am working on the custom sapui5 application development, where i need to display the opened and closed count from the oData Service. Service is working fine in the browser and if i use only opened count it is getting displayed(respectively). But if add both opened and closed  count in my controller, it is not working. Please see the code below.

Code:

var url = "proxy/sap/opu/odata/sap/ZDEFECTLIST_SRV/";

  var oModel = new sap.ui.model.odata.ODataModel(

  url, true, "",

  "", true);

  oModel.read("/IncidentDetailSet/$count?&$filter=Status eq 'OPENED'", {sync : false, 

        success : function(oData, response) { 

             console.log(response.body); //Its a string 

             openCount=response.body;

            

  },

  error : function(oData,response)

  {

  alert("Service Error");

  }}); 

  oModel.read("/IncidentDetailSet/$count?&$filter=Status eq 'CLOSED'", {sync : false, 

        success : function(oData, response) { 

             console.log(response.body); //Its a string 

             closedCount=response.body;

            

  },

  error : function(oData,response)

  {

  alert("Service Error");

  }}); 

  var amModel4 = new sap.ui.model.json.JSONModel({

  'businessData' : [

  {'Status': "OPENED", 'Count': openCount},

  {'Status': "CLOSED", 'Count': closedCount}

  ]

  });

Thank you,

Regards,

JK

Accepted Solutions (1)

Accepted Solutions (1)

Jayakrishnan
Active Participant
0 Kudos

HI All,

Thank you for the comments. Actually i have used a button to display the count in a pie chart. So that i have created the Full screen from the Split-app. I have written the logic in my app.view.js and controller.(i am not using routing). so in my app view i have loaded my chart view in addition to my Master and Empty page. So that it is getting called in only one time. so that if put open and closed service only open service got consumed. after that it never call the service again. now i have removed that full screen code, i have used my detail page to display the chart. now it is working.

So that i am closing this thread

Answers (3)

Answers (3)

SergioG_TX
Active Contributor
0 Kudos

can you try changing your path from:

"/IncidentDetailSet/$count?&$filter=Status eq 'OPENED'"


to


"/IncidentDetailSet?$filter=Status eq 'OPENED'


then, inside your success callback, you can do.. oData.results.length as suggested by Chandan

former_member216578
Participant
0 Kudos

It's a bad idea to pull all the data just to count the number of matches. $count is for this.

Former Member
0 Kudos

Hi JK,

you have 2 options:

use the following code:


  this.getModel().read("/ProductCollection/$count",{

  success: function(oResult){

  // check if the result is a number

  if (isNaN(oResult)){

  var oIntResult = parseInt(oResult);

  }

  }

  });

or grab the count value from the bindings you can see an answer here: SAPUI5 - How to Bind Odata $count to a XML view - Stack Overflow

@chandan - your answer is for counting the dataset which is being returned by the oData. If you want to implement paging (usually for large amount of datasets) you must know the $count value. by design UI5 core execute 2 calls: count all items and query the collection.

Ran.

Former Member
0 Kudos

Hi Ran Hassid,

Thank you for the Info, appreciate it...

I am getting the following error with you code -


The following problem occurred: HTTP request failed415,Unsupported Media Type,<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code /><m:message xml:lang="en-US">Unsupported media type requested.</m:message></m:error> - 

can you try it on th JS bin

Regards,

Chandan

Former Member
0 Kudos

Hi Chandan, this is strange.. i tested in on NetWeaver gateway oData service and it works..

i will try to investigate it .

Former Member
0 Kudos

Hi,

"oData.results.length" will do the trick.

working example - JS Bin - Collaborative JavaScript Debugging

Thanks,

chandan