Skip to Content

Read data from oData model and show them in listBox

Hi guys,

I am trying to create a listbox which contains data coming from HANA Database, using rest services and oData model, so I have came out with the following code to get the data, but I am not sure how to add them/bind them to the listBox in this case.

The code below is working, as I get a success response with the data.

Would you please advise ?

       oModel.read(
            "/Schema(" + "'"+data+"')",
            null, [],
            true,
            function (oData, oResponse) {
            console.log("Success!");
            },
            function (oError) {
            sap.ui.commons.MessageBox.alert("Error ! Username is not found !");
            }
        );
  

Thank you so much for your support,

Shady

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Oct 04, 2014 at 07:56 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.