Skip to Content
0

Update table populated with oData svc dynamically with row index

Oct 25, 2017 at 12:30 PM

45

avatar image

Hi,

I have a table like this: rwtab.jpg. Now I want to update records. So I have written this: rw1.jpg but I am still unable to fetch any row by its index as the index is always returning as null i.e. rwdeb.png. Can anybody help me please? Thanks!

rwtab.jpg (184.1 kB)
rw1.jpg (173.9 kB)
rwdeb.png (152.1 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Jun Wu Oct 25, 2017 at 01:50 PM
0

what you are trying to do? why you need the index?

Show 3 Share
10 |10000 characters needed characters left characters exceeded

I want to update the oData model. I am using: http://services.odata.org/V2/(S(mdnqu2ns3jbw42hgnvya1evn))/OData/OData.svc/

and this is my code and I am getting error: Uncaught TypeError: Cannot read property 'getPath' of undefined

onSaveUpd:function(oEvent){
 
var oModel= this.getView().getModel("venModel");
	var oTable = this.getView().byId("emptab");
  var oselectedItem = oTable.getSelectedItem();
	var aggregations = oTable.getAggregation("items");
	var index = aggregations.indexOf(oselectedItem);
	var sPath = oEvent.getSource().getBindingContext("oModel").getPath(); 
//	var oContext= oTable.getContextByIndex(index);
	var mNewEntry={};
	mNewEntry.ID=this.getView().byId("id").getValue();
	mNewEntry.Name=this.getView().byId("lsname").getValue();
//	oModel.setProperty("/Suppliers('"+index+"')/Name", mNewEntry.Name);
	oModel.update(sPath,mNewEntry,function(oData){
		alert("successful");
	},
	function(err)
	{
		alert("unsuccessful");
	});
		oModel.refresh();
	this.getView().byId("SimpleForm").setVisible(false);
}
});

0

why not two-way binding?

0

Thanks for the reply!

Do you have any sample code or reference?

0
Jun Wu Oct 25, 2017 at 01:10 PM
0

probably you just don't have any row selected...

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Thanks for the response! I have written the code in the table definition as

	<Table id="emptab" items="{ path: 'venModel>/Suppliers'}" mode= "SingleSelect" >
			
0

Then in controller

	this.getView().byId("SimpleForm").setVisible(true);
	this.getView().byId("saveup").setVisible(true);
	var oTable = this.getView().byId("tstable");
	 var oselectedItem = oTable.getSelectedItem();
	var aggregations = oTable.getAggregation("items");
	var index = aggregations.indexOf(oselectedItem);

0

Here is the debugging window

rwdeb.png

I am selecting the radio button

rwdeb.png (152.1 kB)
0

Got it! That was just an incorrect table name!

0

Its now getting the index but still having an error:

Uncaught (in promise) TypeError: Cannot read property 'indexOf' of undefined

and the data is not being updated into the table :(

0
Irfan Gokak Oct 26, 2017 at 07:02 AM
0

Hi Tridwip,

For getting the index of selected row you can do like this on click event.

function : selectFun(oEvent){
   var sPath = oEvent.getSource().getBindingContext("modelName").getPath(); 
   //From this path you can use split() function and get the index
   
   // Get selected row object
   var oSelRow = oEvent.getSource().getBindingContext("modelName").getObject(); 
}
Show 5 Share
10 |10000 characters needed characters left characters exceeded

Here is my code and I am getting the error "Uncaught TypeError: Cannot read property 'getPath' of undefined"

onSaveUpd:function(oEvent){
 
var oModel= this.getView().getModel("venModel");
	var oTable = this.getView().byId("emptab");
  var oselectedItem = oTable.getSelectedItem();
	var aggregations = oTable.getAggregation("items");
	var index = aggregations.indexOf(oselectedItem);
	var sPath = oEvent.getSource().getBindingContext("oModel").getPath(); 
// also tried: var sPath = oEvent.getSource().getBindingContext("oModel/Suppliers").getPath();  
	var mNewEntry={};
	mNewEntry.ID=this.getView().byId("id").getValue();
	mNewEntry.Name=this.getView().byId("lsname").getValue();
	oModel.update(sPath,mNewEntry,function(oData){


		alert("successful");
	},
	function(err)
	{
		alert("unsuccessful");
	});
		oModel.refresh();
	this.getView().byId("SimpleForm").setVisible(false);
}
});
<br>
0

Hi,

Your model name is "VenModel" right?

Please change below line.

var sPath = oEvent.getSource().getBindingContext("VenModel").getPath(); 
0

I am attaching the screenshots. Please help!

ctrl: ctrl.jpg

manifest: man.jpg

ctrl.jpg (205.6 kB)
man.jpg (168.8 kB)
0

If I take this approach the hard coded index is getting updated but how to replace that hard coded index?

onSaveUpd:function(oEvent){
 
var oModel= this.getView().getModel("venModel");
	var oTable = this.getView().byId("emptab");
  var oselectedItem = oTable.getSelectedItem();
	var aggregations = oTable.getAggregation("items");
	var index = aggregations.indexOf(oselectedItem);
//	var sPath = oEvent.getSource().getBindingContext("venModel").getPath(); 


	var mNewEntry={};
	mNewEntry.ID=this.getView().byId("id").getValue();
	mNewEntry.Name=this.getView().byId("lsname").getValue();


      oModel.update("/Suppliers(2)",mNewEntry,function(oData){ //the index 2 record is getting updated
		alert("successful");
	},
	function(err)
	{
		alert("unsuccessful");
	});
		oModel.refresh();
	this.getView().byId("SimpleForm").setVisible(false);
}
0

Its been done! Thanks!

0