cancel
Showing results for 
Search instead for 
Did you mean: 

JSON Model - Table Data access problem

Former Member
0 Kudos

Hi

I am trying to read north wind odata model as below.

var oModel = new sap.ui.model.json.JSONModel("http://services.odata.org/V3/Northwind/Northwind.svc/Products?$format=json");

sap.ui.getCore().setModel(oModel,"northwind");

and I am able to set the values to my table with the below code.

var oTable = new sap.ui.table.Table("tableId",{

  visibleRowCount: 5,

  editable: false

  });

  oTable.addColumn(new sap.ui.table.Column({

  label: new sap.ui.commons.Label({text: "ProductID"}),

  visible: true,

  template: new sap.ui.commons.TextView({text: "{northwind>ProductID}"})

  }));

  oTable.addColumn(new sap.ui.table.Column({

  label: new sap.ui.commons.Label({text: "Product Name"}),

  visible: true,

  template: new sap.ui.commons.TextView({text: "{northwind>ProductName}"})

  }))

  oTable.addColumn(new sap.ui.table.Column({

  label: new sap.ui.commons.Label({text: "Quantity PerUnit"}),

  visible: true,

  template: new sap.ui.commons.TextView({text: "{northwind>QuantityPerUnit}"})

  }));

  oTable.bindRows("northwind>/value");

I am trying to read selected row in the above table with the below code.

var oTable = sap.ui.getCore().byId('tableId');

  //var data[];

  var selected = oTable.getSelectedIndex();

  if(selected ==-1) {

  alert("select a row");

  }else{

  $("#formId").slideDown(function() {

  //var data = oTable.getModel('northwind').oData['Products('+ selected +')'];

  var data = oTable.getModel('northwind').getData();

    

  console.log(data);

and i am able to see the data in browser console.(screen shot attached). But I am unable to read the values from the variable data.

I tried with data.length, data.getProperty(),  but it's not returning any values.

Any help

Thanks

Ravi

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ravi,

try it with data.value.

This seems to be the array with the requested objects

Regards

Daniel

Answers (4)

Answers (4)

santhu_gowdaz
Active Contributor
0 Kudos

Dear Durga,

     Please tel me why it is "Assumed Answer" even though it is a general question and answered many times in this forum.

Please mark this Discussion with a Correct Answer (closes, but does not lock the Discussion) and Helpful Answer where appropriate. Seehttp://scn.sap.com/community/support/blog/2013/04/03/how-to-close-a-discussion-and-why   Even if you discovered the solution without any outside contributions, it helps others to understand what the solution turned out to be. Do not use Assumed Answered as it confuses anyone looking for the specific answer.

santhu_gowdaz
Active Contributor
0 Kudos

"data" is an object. so it's sowing all the data. if you want to get selected row data then go either of this way.

var obj = evt.getSource().getBindingContext("northwind").getObject();

console.log(obj);

console.log(obj.ProductName);

or,

var model=that.getView().getModel("northwind"); var path = evt.getSource().getParent().getBindingContextPath(); var data=model.getProperty(path);


http://stackoverflow.com/questions/21600003/how-to-get-row-details-without-actually-selecting-the-ro...


NagaPrakashT
Contributor
0 Kudos

Hi Durga,

Can you please have a look at these threads, see if they help you.

Thanks,

Naga

santhu_gowdaz
Active Contributor
0 Kudos

See these example to get table data in various methods,

http://jsbin.com/tipokenuki/1/edit?html,js,console,output

http://jsbin.com/kipese/7/edit?html,js,output

And you can see lot of working examples here -

use odatamodel instead of json model. Bec json model is client side so its not able to read the data from that server.

http://scn.sap.com/community/developer-center/front-end/blog/2013/09/12/how-to-read-data-from-a-gate...