Skip to Content

How to display the count from oData Service in SAPUI5

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Jul 21, 2016 at 12:29 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 21, 2016 at 08:02 AM

    Hi,

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

    working example - JS Bin - Collaborative JavaScript Debugging

    Thanks,

    chandan

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 21, 2016 at 12:04 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 21, 2016 at 09:02 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.