cancel
Showing results for 
Search instead for 
Did you mean: 

Getting count of a odata object field and pass the count to a table

Former Member
0 Kudos

Hi Experts,

My requirement is to get the count of the status field for the odata "Service Request" and pass that count value to my table. Please suggest me how this can be accomplished in ui5.

On referring some blogs I am sure I have to use $count but not sure how to apply the same to a odata object field.

Thanks,

Srinivasan

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member203031
Contributor
0 Kudos

Hi srinivasan,

Do you want to know the count of the list which you are getting from the odata?

If yes then you will the list of values in the success (data) there please place a breakpoint and check the data in the console,there you will get some list of objects.

if you check data.length you will get the count of the objects which you fetched....!

Thanks,

Deepak Raj.

Former Member
0 Kudos

Hi Deepak,

Thank you for your reply. I am not looking for the count of the list. Lets say my odata has a field "status" with different values like active, dismantled and so on.

I want the count of the active status from that odata. Hope I am clear regarding my requirement.

Regards,

Srinivasan

Former Member
0 Kudos

Hi Deepak,

In my odata I have a field called "Status" different values like Active, Dismantled and so on. I want to get the total count of this status and count of individual status.

Hope I have made the requirement clear.

Regards,
Srinivasan

former_member203031
Contributor
0 Kudos

Hi srinivasan,

Sorry for the wrong solution.

if you want to get the count of the particular field in the list of objects,then you need to read the complete data and then you have to pass your required fields in to one array then you can take the length of that array and you can pass it to the table.

I hope this will be helpful,if i am wrong please reply me back.

Thanks,

Deepak Raj.

Former Member
0 Kudos

Hi Deepak,

This is my exact requirement. I am also searching for some code reference regarding this. It would be helpful if you can provide any reference from your side.

Thanks,

Srinivasan

former_member203031
Contributor
0 Kudos

Hi srinivasan,

Please take a look on the below piece of code:

                                    var count = [ ] //defining an empty array

                                    var data1 =model.getData().results; //this is the path of my records

                                    for(var i=0;i<data1.length;i++){

                                    if(data1[i].ContactTitle == "Owner"){

                                    var obj = {}

                                    obj.contacttitle = data1[i].ContactTitle;

                                    count.push(obj);                              //pushing the record in to array

                                    }//if

                                    }//for

                                    console.log(count.length);  //here you can get the count

Former Member
0 Kudos

Hi Deepak,

I tried your code and got the below error.

"Cannot read property 'length' of null(…)"

Regards,

Srinivasan

Former Member
0 Kudos

Also I dint understand the ContactTitle part in the If loop

former_member203031
Contributor
0 Kudos

In your case it is the status
In my case it is Contact title...,

Have you tried keeping a breakpoint and check  the values in the console...?

Can you show your code....?

Former Member
0 Kudos

I have attached my onInit() code file here along with the code you suggested. Please do revert back if anything else is required.

Thanks,

Srinivasan

former_member203031
Contributor
0 Kudos

Have you checked in the console the records of data?

Have checked the properties of data in the console?

Former Member
0 Kudos

Yes in the console I get the odata details where I have set the oModel

former_member203031
Contributor
0 Kudos

then what you are passing to the data1

you defined

var data1 = oModel.getData()..?

Former Member
0 Kudos

It is from the code reference you shared.

When I use var data1 = oModel.getData().results;

It shows "Cannot read property 'results' of null(…)"

Hence I removed it and tried using var data1 = oModel1.getData();

former_member203031
Contributor
0 Kudos

I sent you code based on my data and records.

Could you please check data1?

what the data1 contains and where you are getting the data from the server?

Former Member
0 Kudos

Sure Deepak, will check and get back to you.