Supereme
Supereme

Reputation: 2409

Save operation using ODataModel

I am new to SAP ui5. I have successfully shown data using ODataModel to a table. Now I want to update the data in the table back into the database. For this I know that we have something called 'update' function of the model in which we can specify the path and the data. My question here is what if I want to give the data in the form of json? How do I retrieve data in the table in the form of json and have it passed to the 'update' method?

Any help would be appreciated. If possible please share example such.

Thanks

Upvotes: 0

Views: 6164

Answers (1)

qmacro
qmacro

Reputation: 3105

You can use the OData model object to get it. Use the path to retrieve the JSON object, update what you need, and then call the update function. Here's an example:

getDataContext : function(oItem) {
  var sPath = oItem.getBindingContextPath();
  var oModel = this.getView().getModel();
  return {
    path : sPath,
    data : oModel.getObject(sPath)
  };
}

From there you can update the data in mDataContext.data as desired, then call the update:

this.getView().getModel().update(
  mDataContext.path,
  mDataContext.data,
  { // your context, success, error handlers etc }
)

Here's a fully working example that allows you to look at products in Northwind, select one, and increase or decrease that selected product's rating.

Upvotes: 1

Related Questions