Skip to Content
0

How to get the first value from odata response when using filter parameters in SAPUI5?

Feb 16, 2017 at 05:44 AM

1.2k

avatar image

Hi all,

I use XML views for coding. I need to get the first value from the odata response when using filter type of odata binding . Here's the controller code what I have tried:

Controller code:

function(oEvent){

var incrtab = this.byId("idMyApprovalsList");

var oItems =new sap.m.ObjectListItem({

title :"{EmployeeName}" , number:"{AgeingDays}",

numberUnit:"{path: 'CreatedDate',type: 'sap.ui.model.type.Date',formatOptions: {style: 'medium'}}", type : "Navigation",

press: this.handleSOSelect,

attributes:[new sap.m.ObjectAttribute({text :"{EmployeeId}"}),

new sap.m.ObjectAttribute({text :"{ProcessName}"}),

new sap.m.ObjectAttribute({text :"{WorkitemId}", visible:false}) ] });

var filters = new sap.ui.model.Filter("ImUser", sap.ui.model.FilterOperator.EQ, name); incrtab.bindItems("/APPR_PENDING_LISTSet",oItems, null, filters); },

If we use like :

oModel.read(sPath,{ success: function(oData,oResponse)});

Then we can get value as: pcode=oData.PaCode;

But in my case, how can I get the "0"th array data or the first values from the list without pressing the list?

Can Someone help me with this?

Regards,

Ramya

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Mahesh Phajage Feb 16, 2017 at 11:38 AM
1

Hello Ramya Ramasamy,

You have two options to get first element in your odata response:

1) oData.results[0] // here you will get first record in your entity

then get each field

var data = oData.results[0]; / you need to debug when you get success response in oData.read() call.

data.FieldName1;

data.FieldName2;

or (data) you can directly bind to any control that you want.

2) alternative for list:

var list = sap.ui.getCore().byId("listID_name"); // change list id name

var firstRecord = list.getItems()[0].getBindingContext().getProperty();

Hope his will help you!!!

Regards,

Mahesh Phajage

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hi Mahesh,

option 1 cannot be used in my case because, I'm directly binding items to the objectListItem.

I tried the 2nd option in oninit() as you said:

var list = this.getView().byId("idMyApprovalsList"); // change list id name

var firstRecord = list.getItems()[0].getBindingContext().getProperty("EmployeeId");

console.log(firstRecord);

Now I get an error like:

Uncaught TypeError: Cannot read property 'getBindingContext' of undefined

Kindly help me with this.

0
Radek Chudziak Feb 16, 2017 at 09:43 AM
0

As per oData v2:

"The data requested from an OData service is cached in the OData model. It can be accessed by the getData() and the getProperty() method, which returns the entity object or value. These methods do not request data from the backend, so you can only access already requested and cached entities:"

oModel.getData("/Customer('ALFKI')");
oModel.getProperty("/Customer('ALFKI')/Address");

Try to adjust it to your example

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Radek,

Thanks for your reply.

In my case I don't have that oModel. I'm directly binding it to the objectListItem.

Is there any other way to get the value?

0

Yes you do.

var oModel = this.getOwnerComponent().getModel();

This will return you the instance of your global model.

0