cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 Binding problem with Web Ide Odata when changing data

former_member202213
Participant
0 Kudos

Hi everyone,

i'm currently facing the following issue :

I read odata value to load a form. I binded my page using a template.

this way i use dynamic data to fill every fields. this works like a charm :

it is different when I want to change data.

I tried two solutions :

1/ collecting model and changing value :

var oView = this.getView().byId("it_item");
var oBinding = oView.getBindingContext();
var oModel = oBinding.getModel();
oModel.setProperty("input1", dataTable.NameOrg1);
oModel.refresh();

2/ Changing directly data in screen :

this.getView().byId("input1").setValue(dataTable.NameOrg1);

both ways doesn't work.

I fear the problem is that data from screen is not directly binded with data from odata. it's only thanks to the template that it is done.

you can see that in the view :

<App class="sapUiResponsiveMargin" width="auto" id="it_item" pages="{tableData>/ContratC13Set}" cd:w5g.dt.context="tableData>/ContratC13Set">
<Input xmlns="sap.m" id="input1" value="{tableData>Nom1Societe} {tableData>Nom2Societe}"/>

i'm not able to read data from input1 field as data is dynamic and not able to change it either.

here is my init method to bind odata to view :

			this.getView().setModel(odataModel, "odataModel");
			this.getView().setBindingContext(new sap.ui.model.Context(odataModel, "/ContratC13Set"));

do you have any solution to help me ?

do I need to affect a Model to my field so that I can read it and change it's value ?

Accepted Solutions (1)

Accepted Solutions (1)

beanonradar
Explorer
0 Kudos

The first argument of oModel.setProperty must be the path of the entity that you want to change. In your case it would be:

var oModel = this.getView().getModel("tableData");
oModel.setProperty("/Nom1Societe", "new value of Nom1Societe");

or

oModel.setProperty("/Nom2Societe", "new value of Nom2Societe");
former_member202213
Participant
0 Kudos

Dear Timo,

i'm afraid it doesn't work.

in console, "false" is displayed at the instruction :

oModel.setProperty("/Nom1Societe", dataTable.NameOrg1);

if I do that : oModel.getProperty("/Nom1Societe"), it is "undefined".

maybe it has to do with the way I defined the model ?

beanonradar
Explorer
0 Kudos

Dear Jean-Francois,

can you find the path "/Nom1Societe" in aBindings in your oModel object? Can you attach a screenshot with the expanded section of [0 ... 99]?

former_member202213
Participant
0 Kudos

Yes It is present among others :

former_member202213
Participant

Dear Timo,

after some research and thanks to your information, I finally found the solution :

var sPath = this.getView().byId("it_item").getBinding("pages").aKeys[0];
var oModel = this.getView().getModel("tableData");
oModel.setProperty("/" + sPath + "/Nom1Societe", dataTable.NameOrg1);

The variable sPath permit to collect data binded from last screen where I selected my key Contract.

that is to say sPath equals "ContratC13Set('29')". by concatenation I have access to property "/Nom1Societe".

thanks a lot.

Answers (1)

Answers (1)

mariusobert
Developer Advocate
Developer Advocate
0 Kudos

Hi Jean-François,

Why do you set a binding context manually, is there a specific reason (it should work without the second line)?

Can you show how the model is defined or? Or do you see any errors in your browser?

former_member202213
Participant
0 Kudos

Dear Marius,

I tried without the second line and my code doesn't work anymore.

here is how my model is defined :

"models": {"i18n": {"type": "sap.ui.model.resource.ResourceModel","settings": {"bundleName": "Oa_solaire_C13.Oa_solaire_C13.i18n.i18n"}},"tableData": {"uri": "/sap/opu/odata/sap/ZOA_SOLAIRE_C13_SRV/","type": "sap.ui.model.odata.v2.ODataModel","settings": {"defaultOperationMode": "Server","defaultBindingMode": "OneWay","defaultCountMode": "Request"},"dataSource": "ZOA_SOLAIRE_C13_SRV","preload": true}},

I use the Two lines to define the context because without it the following code doesn't work :

		onSave:function(oEvent){
			var lvError ="Erreur";
			var lvWarning ="Info";
			var errorData =[];
			var errorExist = false;
			var warningExist = false;
			var oView = this.getView().byId("page0");
			var oBinding = oView.getBindingContext();
			var oModel = oBinding.getModel();

i think I need the following :

			this.getView().setBindingContext(newsap.ui.model.Context(odataModel,"/ContratC13Set"));

or is there a way to bind the context with the view otherwise ?

and how can I have access to data from my screen as they are filled dynamically and furthermore modify it ?

i don't see any error in F12 browser (chrome). but if I try in the console to write oModel.setProperty("input1", dataTable.NameOrg1);, i received the answer false :

it seems normal since I can't find property "input1" in my model :