cancel
Showing results for 
Search instead for 
Did you mean: 

how to get the count property from the oData response

ashish_bansal
Explorer
0 Kudos
3,014

Hi,

I have a fullscreen view in my Fiori app where we call a oData service through table.bindItems() inside the controller.

The binding of items works fine but we have a situation when we need to display the total number of items (for e.g. 515) on the UI. We do not get this information from the VDM, so we plan to use the length property of the getList() method to determine the total number of items. But because of growing feature of the table it only reads 100 items at a time and the length property returns only 100. I have attached the listener attachRequestCompleted() for the model so that once the bindItems() request is complete i can get the length property. But my problem is that i want the total count.

I saw in the debug that when this table.bindItems() gets called it makes a oData service call and in the response i can see that there is a count property available which has the total number of items (515 for my e.g.) but how do i read this count property from the response inside my controller (probably inside my attachRequestCompleted)?? I would really appreciate any help in this regard. Thanks.

Kind Regards,

Ashish

Tags edited by: Michael Appleby

Accepted Solutions (0)

Answers (3)

Answers (3)

0 Kudos

Hi,

Set default count mode of Odata Model as Inline.

var sUrl = "gateway/url";

var model = new sap.ui.model.odata.v2.ODataModel(sUrl, true);

model.setDefaultCountMode("Inline");

If the count mode is Inline, then odata will give the results in the below format.

d: {

results:[{},{}],

__count: "1000"

}

__count will be having total records.

Note: Make sure that $count is implemented in the service.

We can retrieve this results in the afterDataRecevied method.

Thanks,

Bhavya

0 Kudos

Hi Ashish,

We are currently facing this issue, did you get any solution how to solve it?

Regards,

Madhuri

ashish_bansal
Explorer
0 Kudos

Hi Madhuri,

We have done it in following way:

this.byId("pecTableOverview").getGrowingInfo().total;

and this should be done inside attachRequestCompleted() listener, which gets called when the model is loaded completely.

I hope this will help.

Regards,

Ashish

Former Member
0 Kudos

I think it is really inconvenient that there is not a function on the model.

I am using a normal sap.ui.table.Table and I haven't been able to find solution yet. I might have to try it with an AJAX request....

Qualiture
Active Contributor
0 Kudos

Not true -- there is certainly a method to get the complete count (i.e. not limited by model maxSize or GrowingList tresholds: retrieve the total length from the binding

For example:


var oBindings = this.getView().getModel().bindList("/Customers");

console.log(oBindings.getLength());

will return the correct total

Former Member
0 Kudos

Nice idea Robin, I thought that this would trigger a read but apparently it doesn't. Nice

I tried a different approach that works too:


oModel.read("/MSGSet/$count", {async : false,

       success : function(oData, response) {

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

}});

Qualiture
Active Contributor
0 Kudos

Yeah, I like how retrieving the length from the binding in my example saves you from an extra server roundtrip

Former Member
0 Kudos

I really like the idea and will use it. Thank you

It's always "nice" to see, that some stuff has more methods, functions or parameters as there are mentioned in the API

ChandrashekharMahajan
Active Contributor
0 Kudos

Hi,

Use setSizeLimit to get the all records.

refer JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.Model

also $count should display the count of records avaialable in OData entity collection for eg. http://services.odata.org/OData/OData.svc/Categories/$count

Regards,

Chandra

ashish_bansal
Explorer
0 Kudos

HI Chandra,

Thanks for your suggestions.

But i think in my case we do not want to get the complete list. We still want to get only 100 items initially and then if the user would scroll down he/she would see more items. But we already need to know the total number of items for the purpose of showing a message to the user. For e.g. Total issues (515),  but the table would initially show only 100 and then another 100 after scroll. So setting the size litmit to max would not help.

And to be able to use the $count you mentioned, we have to still make another oData service call which we do not want.

But as i mentioned there is already a property named "__count" in the response of the bindItems() method call and i see that it has correct count but not sure how to retrieve it inside the controller. Thanks.

Regards,

Ashish