Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8533

Re: Read data from oData model and show them in listBox

$
0
0

Hi Shady,

 

You can bind the data to listbox using bindAggregation method. I am assuming the structure of oData object as follows:

 

oData = {              items : [ {ProductName : "Laptop" },                           {ProductName : "Mobile" },                         .......                              ]
};

Now in controller you can bind the response of your oModel.read as follows:

 

var oListBox = this.byId('myListBox'); //this refers to view's controller
oModel.read(          "/Schema(" + "'"+ data + "')",          null, [],          true,          function(oData, oResponse) {               var oItem = new sap.ui.core.ListItem({                                        text : "{ProductName}"                                   });               var oJSModel = new sap.ui.model.json.JSONModel(oData);               oListBox.setModel(oJSModel,"myModel");               oListBox.bindAggregation("items", {                                                                           path:"myModel>/items",                                                                           template : oItem                                                                      });          },          function (oError) {               sap.ui.commons.MessageBox.alert("Error ! Username is not found !");          }     );

This same thing can also be done via view binding if you bind the oData model to the view. While defining the control in the view you can specify the entity set path and the SAP UI5 runtime will take care of triggering the read call for the respective entityset.

 

e.g. View Code looks like:

<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"   controllerName="xmlodatamodel.ListBox" xmlns:html="http://www.w3.org/1999/xhtml">  <Page title="Title">  <content>  <Select id="mySelect" items="{/Categories}">  <core:Item text="{CategoryName}"></core:Item>  </Select>  </content>  </Page></core:View>

Controller Code looks like :

sap.ui.controller("xmlodatamodel.ListBox", {  onInit: function() {  var serviceurl = "proxy/http/services.odata.org/V3/Northwind/Northwind.svc/"  var oModel = new sap.ui.model.odata.ODataModel(serviceurl,true);  this.getView().setModel(oModel);  },
});

Regards,

Parag


Viewing all articles
Browse latest Browse all 8533

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>