cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 OData v4 - How to change the model (property value)

maklucksap
Explorer
0 Kudos

Hi guys,

I am a SAPUI5 Beginner and I have a problem which is hopefully easy to solve, but right now I don't know how.

My Requirement:
I am displaying a list of items, which is coming from an OData V4 Model. Let's say these are the entity fieldnames/properties: FieldID, SomeText, Status

The table offers single select and below is a button which should trigger an action for the selected line. Once the user hits that Button I want to change the "Status"-Property and send it to the backend. I already know how to send updates via Odata V4 to the backend when I use Input Fields. I just don't know how to change a Model Property programatically within a JavaScript function, without having the Property linked to an Input Field Control.

When the Button was clicked I call the "Press" Event:

onButtonPressed: function (oEvent){
  var oBinding = this.byId("myTableID").getBinding("rows"),
      aContext = oBinding.getContexts( ),
      oContext = aContext[0], //hard coded for now (returns the first line)
      sStatus = "Accepted"; //New value for Status Property
      
      oContext.?????
}

What do I have to code to change the "Status"-Property in the model? Is it correct that the Context Object can help me achieving this?

Kind Regards
Mark André

P.S.: I saw in the API Reference that Class sap.ui.model.odata.v4.Context offers a "requestProperty"-Method. Sounds interesting but I don't know how to access the property with that method, as it just returns a Promise. I am not familiar with Promises. I think I can attach an "success"- and an "error"-Method to it, but still I don't know how to get and manipulate the Property then.

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member227918
Active Contributor
0 Kudos

try below, should work.

onButtonPressed:function(oEvent){
  var oBinding = this.byId("myTableID").getBinding("rows"),
      aContext = oBinding.getContexts(),
      oContext = aContext[0],//hard coded for now (returns the firstline)
      sStatus ="Accepted"; //New value for Status Property
      
      var bindingPath = oContext.getPath();

      //now get your model
      //if your model in manifest then oModel = this.getOwnerComponent().getModel()
      //if you have assigned a model to your table then, oModel = oTable.getModel()
      // pass model name if you have named model i.e. .getModel("modelName")

      oModel.setProperty(bindingPath + "/Status", sStatus);
}
maklucksap
Explorer

Unfortunately this is not working. There is no "setProperty"-Method in the OData V4 Model class.

0 Kudos

Do you guys have the answer already? I have the similar case also

maklucksap
Explorer
0 Kudos

Once I find the solution, I will post how to solve it. Right now still I don't know how to do it.

0 Kudos

Hi Mark,

Inside press event you just try following code:

oEvent.getSource().getBindingContext("your-model-name").setProperty("/property-name","property-value");

Hope it will help ,

Thanks,

Regards,

Rajvardhan Thakare

maklucksap
Explorer
0 Kudos

Hi Rajvardhan,

thank you for the quick response, but unfortunately this is not working. Method getBindingContext returns undefined.

I already have access to the OData Model, the Binding and the Context Objects and "read access" to the data itself. The problem is that in OData v2 the Model (sap.ui.model.odata.v2.ODataModel) has a setProperty Method, but this method doesn't exist in the OData v4 Model Class (sap.ui.model.odata.v4.ODataModel).


SAP Help says: "The OData V4 model only supports data access using bindings. It does not provide any direct access to the data."

Because of that I thought that one of these classes has to be used to change properties in the model:

- sap.ui.model.odata.v4.ODataListBinding

- sap.ui.model.odata.v4.Context

Am I completely heading in the wrong direction? Can't see any methods that would change the properties.

Kind Regards
Mark André