M.Mikuš
M.Mikuš

Reputation: 51

How to get property from JSON model filled with oData?

Im trying to get property from oData return model. I set data in success callback function from oData to JSON model.

  oODataModel.read("/ConnObjSet?$filter=Objecttype eq 'CONNOBJ' and ConnObject eq '20000000002'",
                        true,
                        true,
                        false,
                        function _OnSuccess(oData, oResponse){
                        var oJSON = new sap.ui.model.json.JSONModel();
                        oJSON.setData(oData);                           
                        sap.ui.getCore().setModel(oJSON, "ConnectionObject");
                        },

This is my JSON object in console log and highlighted property I want to get. I want to get every 15 Buspartner number from whole array.

enter image description here

And this is what I tried to get property:

 var oLog = sap.ui.getCore().getModel("ConnectionObject").oData.results;

 console.log(oLog);

Upvotes: 0

Views: 3622

Answers (3)

A.vH
A.vH

Reputation: 1026

You could also try to add a filter and select to your oData.read

The oData-URL http://services.odata.org/V2/Northwind/Northwind.svc/Products selects all Product with all their properties

http://services.odata.org/V2/Northwind/Northwind.svc/Products?$filter=UnitsInStock%20eq%2017

shows only Products with "UnitsInStock=17"

http://services.odata.org/V2/Northwind/Northwind.svc/Products?$select=ProductID&$filter=UnitsInStock%20eq%2017

selects only the ProductID of Products with "UnitsInStock=17"

so

oODataModel.read("/ConnObjSet?$select=Buspartner&$filter=Objecttype eq 'CONNOBJ' and ConnObject eq '20000000002'"
...

should get the filtered Buspartners directly.

Upvotes: 1

A.vH
A.vH

Reputation: 1026

If you have an array of objects, you can get an array of properties from each of these objects by using the Array.map() function.

So in your case:

var aResults = this.getView().getModel().getProperty("/results");
var aBuspartner = aResults.map(function (r) { return r.Buspartner});
var oJSONModel = new sap.ui.model.json.JSONModel();
oJSONModel.setProperty("/resultarray", aBuspartner)

Upvotes: 1

Henning Rettenmaier
Henning Rettenmaier

Reputation: 53

Please try:

var aResults = this.getView().getModel().getProperty("/results");
var oJSONModel = new sap.ui.model.json.JSONModel();
oJSONModel.setProperty("/resultarray",new Array())
for(var i = 0; i<aResults.lenght;i++){ 
    oJSONModel.getProperty("/resultarray").push(aResults[i].Buspartner) 
}

Upvotes: 1

Related Questions