cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to pass service URL in the OModel object. Not getting the table data in the SAPUI5 app

Former Member
0 Kudos

Hello Experts,

Grettings! I am facing problem while creating the SAPUI5 application.

I created an oData service, it is working fine getting the metadata as well as the json format. But when I am trying to pass that service URL in the oModel object, I am not able to retrieve the table data in the UI app yet there is data in the table. I am not able to figure out why it is not fetching the table data.

The service i passed in the oModel is as follows :

https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/

Please guide me through this query.

It would be really helpful if I can get a solution to this problem.

Thanks in advance.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

pls try this

onAfterRendering: function(){ var that = this; this.oModel = new sap.ui.model.odata.ODataModel("url" );

this.oModel.read("/EntitySet", null, null, false, function(oData, oResponse)

{ var jModel = new sap.ui.model.json.JSONModel(oData);

that.getView().setModel(jModel, "sapremote"); });

Former Member
0 Kudos

I tried the code which you gave.
I am getting a console error : Uncaught TypeError: Cannot read property '0' of undefined

Still not working. I tried to fix this error but was not able to do.

Former Member
0 Kudos

Hi Rajvir,

I dont have access to load the meta data of your url. Could you pls provide the meta data screen shot of your url.

Seems like issue with entity set you are giving is incorrect?

Former Member
0 Kudos

Hey Rahul,

I have attached the metadata url screenshot. Let me know if you need anything else.
Thank You
metadata.jpg

Former Member
0 Kudos

When you call the odata service form UI, did you debug in which function the control is going to success or in error?

When you tried to execute the url from sap gateway are you getting some results?

Few suggestions:

1. Install a postman app for chrome and give the url link and try to post or a get request. if it gives some results which means the back end part is working fine OR

2. Go to gateway and execute the service and check if you are getting the results

3. If above 1 or 2 is returning some results please check the binding part.

Or please paste the code how you are posting it ?

Former Member
0 Kudos

Hey Rahul,

Thanks for the response. I passed the url in the postman app but getting error. I have attached the screenshot of the postman as well as attaching my code. See if you can help me get through it

.snap1.jpg

Controller code


onInit: function() {


		var oModel =  sap.ui.model.odata.ODataModel("https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/")
		
		sap.ui.getCore().setModel(oModel, "products");
		console.log(oModel);
	},


mode:0,
	resetForm: function(){
		$('#name').val('');
		$('#genre').val('');
		$('#id').val('');
	},
	
	create: function() {
		this.mode = 'create';
		this.resetForm();
		
		$('#formid').slideDown(300, function(){
			
			var id = sap.ui.getCore().byId('tableid')._getRowCount();
			$('#id').val(id);
			
		})
	},
	
	edit: function(){
		this.mode = 'edit';
		
		var oTable = sap.ui.getCore().byId('tableid');
		
		var selected = oTable.getSelectedIndex();
		
		if(selected ==-1) {
			alert("select a row");
		}else{
			
			$('#formid').slideDown(function() {
				
				var data = oTable.getModel('products').oData['Products('+ selected +')'];
				
				var id = data.ID;
				var genre= data.Genre;
				var name = data.Name;
				
				$("#name").val(name);
				$("#genre").val(genre);
				$("#id").val(id);
				
			})
			
		}
	},
	
	removeId: 0,
	
	remove: function(){
		
		this.mode = 'delete';
		
		var oTable = sap.ui.getCore().byId('tableid');
		
		var selected = oTable.getSelectedIndex();
		
		if(selected ==-1){
			
			alert("Select a row");
		}else{
				var data = oTable.getModel('products').oData['Products(' + selected + ')'];
				this.removeId = data.ID;
			 	this.save();
		}
	},
	
	save: function(){


		var requestObj = {
				
				requestUri : '',
				method: '',
				headers : {
					
					"X-Requested-With" : "XMLHttpRequest",
					"Content-Type": "application/json;odata=minimalmetadata",
					"DataServiceVersion" : "3.0",
					"MaxDataServiceVersion" : "3.0",
					"Accept": "application/json;odata=minimalmetadata"
				}
		} ;
		
		
		var newData = {
				
				"odata.type": "ODataDemo.Product", //from metadata info
				"ID":$('#id').val(),
				"Name":$('#name').val(),
				"Genre":$('#genre').val(),
		}
		
		if(this.mode == 'create'){
			var url = "https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/Test";
			var method= "POST";
			
			requestObj.requestUri = url;
			requestObj.method = method;
			requestObj.data = newData;
		}
		
		else if(this.mode == 'edit') {
			
			var id = $('#id').val();
			var url = "https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/Test("+id+")";
			var method = "PUT";
			
			requestObj.requestUri = url;
			requestObj.method = method;
			requestObj.data = newData;
		}
		
		else if(this.mode == 'delete'){
			var id= this.removeId;
			var url = "https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/Test(" + id + ")";
			var method= "DELETE";
			
			requestObj.requestUri = url;
			requestObj.method = method;
		}
		
		OData.request(requestObj, function(){
			
			sap.ui.getCore().getModel('products').refresh();
			$('#formid').slideUp();
		});
	}
});

//This is view where i created the table and the bind


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: "ID"}),
visible:true,
template: new sap.ui.commons.TextView({text: "{products>ID}"}) }));
oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "Name"}),
visible:true,
template: new sap.ui.commons.TextView({text: "{products>NAME}"}) }));
oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "Genre"}),
visible:true, template: new sap.ui.commons.TextView({text: "{products>GENRE}"}) }));
oTable.bindRows("products>/TestApp")


Thanks in advance!
junwu
Active Contributor
0 Kudos

how you bind the table to the model? how you try to get the data?

Former Member
0 Kudos

I am a beginner to this. This is my code where I have bind the table

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: "ID"}),

visible:true,

template: new sap.ui.commons.TextView({text: "{products>ID}"}) }));

oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "Name"}),

visible:true,

template: new sap.ui.commons.TextView({text: "{products>NAME}"}) }));

oTable.addColumn(new sap.ui.table.Column({ label: new sap.ui.commons.Label({text: "Genre"}),

visible:true, template: new sap.ui.commons.TextView({text: "{products>GENRE}"}) }));

oTable.bindRows("products>/TestApp")

This is the Controller code :

var oModel = sap.ui.model.odata.ODataModel("https://i0xb0bcbe732.us1.hana.ondemand.com/Test1/odataservice.xsodata/");

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

Thank you for your response!

junwu
Active Contributor
0 Kudos

how you try to get the data?