Skip to Content

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

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.

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Posted on May 28, 2019 at 12:13 PM

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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Apr 12, 2019 at 10:50 AM
    -2

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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é

  • Posted on May 28, 2019 at 01:26 PM
    -2

    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);
    }
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.