cancel
Showing results for 
Search instead for 
Did you mean: 

How to read the odata result in sapui5

former_member225463
Participant
0 Kudos

Dear Experts,

I am new to the XML Views , and stuck in one place.

Currently I am doing an Master Details Fiori Application and binding the Header data to the list of the master view.

the data is coming correctly in my master View.

Now i have a requirement that i have to read the odata result in one of the json model  , in the master controller but i am not getting how to get it.

In the JS view , we use to set the json model using the code

oJSONDetailModel = new sap.ui.model.json.JSONModel();

  oJSONDetailModel.setData({

  modelDataDetail : oJSONModel.oData.modelData[0]

  });

But not getting how to do it in XML Views.Please help.

Regards,

Vikash

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Vikash,

You have to use oJSONModel.read().

oJSONDetailModel = new sap.ui.model.json.JSONModel();

oJSONModel.read(path,null,null,false,function(oData,oResponse){

oJSONDetailModel.setData(oData);

},

function(){

console.log('Read failed');

});

Regards,
Naren L Naik

karthikarjun
Active Contributor
0 Kudos

Hi Naren,

Correct me if I was wrong.

JSON Model is a client side model. So that it don't have a property called read. JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.json.JSONModel

ODATAModel - It is a server side model, so here we have an options called read, update, create and delete. JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.ODataModel

Regards,

Karthik A

Former Member
0 Kudos

HI Karthik,

yes it is correct. JSON model is a client side model and it doesn't have read method.

I had assumed that oJSONModel is a variable which looked something like this

oJSONModel=new sap.ui.model.odata.ODataModel(path);

SInce Vikash had posted he was setting the odata model initially and then trying to get the result.

Hope that made sense.

Regards,

Naren

karthikarjun
Active Contributor
0 Kudos

Hi Vikasha,

1.Here is the code that I am using in onInit method.

eg:

var sURI = "https://cors-anywhere.herokuapp.com/http://services.odata.org/V3/Northwind/Northwind.svc/";

      var oView = this.getView();

       var oDataModel = new sap.ui.model.odata.ODataModel(sURI);

       oView.setModel(oDataModel);


2. Then create a new controls in view.

3. Bind the elements from odata to view

eg:

new sap.m.Input({

value : "{/Employees/0/Title}"

})

Hope this will helpful for your requirement.

Regards,

Karthik A

former_member225463
Participant
0 Kudos

Hello Karthiik and Naren,

The answer which you have told works perfectly for JS views , but i want to have the code for te XML View.

My Query is for the first time when the master view gets loaded the odata is called and i want to have the same odata result in one of my controller say on search function. I dont want to call again the Odata.

Regards,

Vikash

Former Member
0 Kudos

Hi Vikash,

So you want to access the set odata in different function or methods in your controller?

you could use this.getView().getModel() within your controller function.

If that does not help could you post how you are setting the odata model initially.

Regards,
Naren L Naik

karthikarjun
Active Contributor
0 Kudos

Will this helpful? http://plnkr.co/edit/5vZRfTLGCHV69gxVL9m3?p=preview

Credits go to

Regards,

Karthik A

former_member225463
Participant
0 Kudos

Hi Naren,

Am able to fetch the model used in the view.

As you suggest te read operation that is one way i got it but is there a way to bind the Odata result that has been called initially when the Master Page has been binded.

I dont want to make a different call but want to use the previous called odata result.

Since i have to do filter what ever the result i got in the odata result , so i want to use the odata result in a json model and there i want to use the filter operation.

Hope my query is clear and thanks for the tip.

Regards,

Vikash

Former Member
0 Kudos

Hi Vikash,

Coul you share the code snippet for the view and controller where you are setting the model to the Page?

Regards,
Naren L Naik

karthikarjun
Active Contributor
0 Kudos

Hi Vikash,

Walk-through this sample code. http://plnkr.co/edit/5vZRfTLGCHV69gxVL9m3?p=preview

It is not recommended to create json model. you can reuses the same model as what you called from controller.

Steps to follow:

1. Bind the date to core or view. sap.ui.getCore().setModel(your model name, "MyModel");

Now data are available under 'MyModel'.

2. By using sap.ui.getCore().getModel("MyModel").getData() // you can use this code to get data

Let us know if you need any further information.

Regards,

Karthik A

former_member225463
Participant
0 Kudos

Hi Naren,

Please find out my code :

mvc:View controllerName="indent.view.Master" xmlns:core="sap.ui.core" xmlns:footerbar="sap.ushell.ui.footerbar" xmlns:mvc="sap.ui.core.mvc"

  xmlns="sap.m">

  <Page id="masterPage" title="{i18n>masterTitle}">

  <subHeader id="masterSubHeader">

  <Bar id="searchBar">

  <contentMiddle>

  <SearchField id="searchField" placeholder="{i18n>masterSearchPlaceholder}" liveChange="onSearch" showRefreshButton="{False}"

  tooltip="{i18n>masterSearchTooltip}" width="100%"></SearchField>

  </contentMiddle>

  </Bar>

  </subHeader>

  <content>

  <List growing="true" growingScrollToLoad="true" id="list" items="{/OrderHeaderSet}" mode="{device>/listMode}"

  noDataText="{i18n>masterListNoDataText}" select="onSelect">

  <items id="masterList">

  <ObjectListItem counter="0" id="mainListItem" markFavorite="false" markFlagged="false" number="{Status}" numberUnit="" press="onSelect"

  showMarkers="false" title="{Orderid}" type="{device>/listItemType}">

  <core:ExtensionPoint name="extListItemInfo"/>

  </ObjectListItem>

  </items>

  </List>

  </content>

  <footer id="masterFooter">

  <Toolbar id="masterToolbar">

  <content>

  <footerbar:SettingsButton icon="sap-icon://action-settings" id="settingsButton" tooltip="Help"></footerbar:SettingsButton>

  </content>

  </Toolbar>

  </footer>

  </Page>

</mvc:View>

Here you can see in my view i have binded the item with the  orderheaderset which is called when the view is called initially.

The service url has been declared in component.js and the global model is oModel which has been set in my component.js.

Now in my controller i want to use the result of this odata result which i will be passing to json model so i can achieve the client side filteration for the search bar.

But i am not geeting the point how to capture the odata result in my onsearch () controller.

Regards,

Vikash