cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to bind textfield value to OData for getEntity

amber_garg
Active Participant
0 Kudos

I have an Odata service with getEntity and getEntitySet methods implemented for an Employee entity. The following screenshot shows the xml output of getEntity in browser.

Now from the same method , I want to populate a text field in UI5 with the value of "Name" . For that I created oDATA model instance but I m not able to understand how to call a single entity. I tried with SAP Demokit but in all the examples , they dont show the sample XML data from which they are extracting the data making it difficult to understand which xml tags and path they are binding to an element. Hence I am not getting a clue on how to achieve my above requirement using getEntity .


createContent : function(oController) {

    var oData = new sap.ui.model.odata.ODataModel("http://ehp7prd.sap.in:8200/sap/opu/odata/sap/ZEMPLOYEE_SRV/",false,"fioriconfig","Bharath123");

    //oData.read("EmplSet('002')");

    sap.ui.getCore().setModel(oData);

       var oFlowLayout = new sap.ui.layout.ResponsiveFlowLayout({

     responsive : true  // boolean

       });

       var inp1 = new sap.ui.commons.TextField("txt1");

       inp1.bindProperty("value","/EmplSet('002')/Name");

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

anyhing in dev console?

amber_garg
Active Participant
0 Kudos

Console and Network tabs do not show any error.

Is the code i have written above correct or I need to do something else to call GetEntity method of the service. Is the inp1.bindProperty("value","/EmplSet('002')/Name");   correct as per the xml?? Kindly let me know.

former_member182372
Active Contributor
0 Kudos

inp1.bindProperty("value","/EmplSet('002')/Name"); looks ok to me


do you see $metadata file loading in Network tab?

amber_garg
Active Participant
0 Kudos

Yes , $metadata file can be seen without any errors in Network Tab although with 304 status not modified. Please find 2 screenshots below

amber_garg
Active Participant
0 Kudos

Once again I refreshed the page , and this time metadata file was shown with 200 status and not 304 as shown above in screenshot.i

And one more important thing I missed to tell is that, i tried putting external breakpoint in my get_entity method and its NOT getting triggered. However if i uncomment the line //oData.read("EmplSet('002')");

then the breakpoint inside getEntity method is triggered. So that means link and connectivity issues are not there otherwise in case of oData.read would not have triggered the breakpoint.

Any soulutions people??

Regards



Private_Member_15166
Active Contributor
0 Kudos

Is this helpful?

              var oModel = new sap.ui.model.odata.ODataModel("http://ehp7prd.sap.in:8200/sap/opu/odata/sap/ZEMPLOYEE_SRV/");

              sap.ui.getCore().setModel(oModel);

                          var inp1 = new sap.ui.commons.TextField("txt1");

                          var sRead = "/EmplSet(Empno='002')"  ;

              oModel.read( sRead, null, null, true, function(oData, oResponse){

                          inp1.setValue(oData.Name);

              },function(){

            alert("Read failed");});

amber_garg
Active Participant
0 Kudos

Hi,

Thanks a lot for your reply. I will have the system access at night so I will be able to test it out that time and let you know the result. Meanwhile I got few doubts regarding your solution.

1)So in the above code . bindProperty method is not required??? If not then whats the difference between the two ways a) by usinng read b) by using bindProperty

2) Also according the to API reference for read method . following parameters can be passed .

read(sPath, mParameters?) : object


Parameters:

{string}sPath
{map}mParameters?
{object}mParameters.context?
{map}mParameters.urlParameters?
{boolean}mParameters.async?, Default: truetrue for asynchronous requests.
{array}mParameter.filters?
{array}mParameter.sorters?
{function}mParameters.success?
{function}mParameters.error?

In your above example when we pass the following parameters , the boolean parameter is passed as 4th parameter whereas in the above list its 5th parameter . So do we need to have an extra comma before that?? . Similarly after the boolean parameter there are 2 array parameters filters and sorters followed by the 2 functions , so in the code below do we need to have 2 commas before the function to indicate that we are not passing anything to the filters and sorters array.

I mean how the system is going to know which variable/object we are going to pass for which parameter if the sequence and no of parameters are not exactly same. Kindly let me know on this

oModel.read( sRead, null, null, true,function(oData, oResponse){

                          inp1.setValue(oData.Name);

              },function(){

            alert("Read failed");});


Thanks a lot


Private_Member_15166
Active Contributor
0 Kudos
  1. Okay. When we start working on SAPUI5 means you are getting many ways to do the same task. Both methods are right. There might be a third option as well. SAPUI5 provides a lot of flexibility to the developers.
  2. JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.ODataModel

         Here i checked that 2nd parameter is referring to below provided parameters.

{map}

mParameters?

Optional parameter map containing any of the following properties:

Check this once and let me know for any issues.

amber_garg
Active Participant
0 Kudos

Thanks a lot buddy , that worked . So basically now in real time I will be getting the employee id "002" dynamically either from user input and then i will have to concatenate this into the URL string and pass it to read function , i suppose , right???

Thanks again

Private_Member_15166
Active Contributor
0 Kudos

Yes.

Answers (3)

Answers (3)

amber_garg
Active Participant
0 Kudos

Thanks everyone who helped me out here . Dhananjay and Seungchul , both your solutions worked for me . So i came to know now two of the ways by which u can retireve data for Odata

1) using read() function for oModel  as shown by Dhananjay

2) using bindProperty as shown by Seungchul

So now just wanted to know which of these method to apply in which scenario and what is the preferred method to be used generally in odata out of these 2 and if any performance related things to be considered while deciding the same.

Many thanks everyone

former_member232384
Participant
0 Kudos
  1. createContent : function(oController) { 
  2.     var oData = new sap.ui.model.odata.ODataModel("http://ehp7prd.sap.in:8200/sap/opu/odata/sap/ZEMPLOYEE_SRV/",false,"fioriconfig","Bharath123"); 
  3.     //oData.read("EmplSet('002')"); 
  4.     sap.ui.getCore().setModel(oData); 
  5.        var oFlowLayout = new sap.ui.layout.ResponsiveFlowLayout({ 
  6.      responsive : true  // boolean 
  7.        }); 
  8.        var inp1 = new sap.ui.commons.TextField("txt1"); 
  9.        inp1.bindElement("/EmplSet('002')");
  10.        inp1.bindProperty("value","Name"); 
antonette_oberholster
Active Contributor
0 Kudos

Hallo Amber

Maybe you could try something like this for your model?

var idModel = new sap.ui.model.odata.ODataModel(myUrl, false);

idModel.read("employeeSet(filter1='x',filter2='y')",

null,

[],

true,

function(oData, oResponse) {

  var empData = {

   employee : oData.id,

  };    

  var idModel = new sap.ui.model.json.JSONModel();

  idModel.setData(empData);

  sap.ui.getCore().setModel(idModel, "idDataModel");

},

  function(oEvent) {

}

);

txfEmp =  new sap.ui.commons.TextField().bindProperty("text", {path: "empData >/employee"});

Regards

Antonette

amber_garg
Active Participant
0 Kudos

H Antonette,

Thanks a lot for your reply. I just have a slight concern here that is there anyway I can avoid using JSON model in the above case . Or can the above be written using XMLmodel instead of Json.

Also var idModel is declared twice , will it create any issues?

In this txfEmp =  new sap.ui.commons.TextField().bindProperty("text", {path: "empData >/employee"});

the bindProperty would be on value or text since i want to bind the textfield value with this odata. And i guess /employee refers to entity name which in my case is /Empl so probably i will replace /Empl with /employee , right??


I will have access to system at night only hence I will be able to check all these that time only.


Regards