cancel
Showing results for 
Search instead for 
Did you mean: 

how to add empty row to oData binded table without having to save it in the backend first?

Former Member
0 Kudos

Hi Community,

I need to your help again.

I have a grid table binded to oDataModel, which is working fine and showing data as expected. Now I need your help to implement the creation of new entry.

Basically, when the user press "Add Cost Type", a new empty row should be added to the bottom of the table. This row should get saved to backend once the user press "save".

I have seen several examples with JSONModel but didn't had much luck with ODataModel. Any help is highly appreciated.

Here is my "Add Cost Type" function:


var oModel = this.getView().getModel();

  var rowCount   = this.getView().byId("idCostPerUnitsTable").getBinding("rows").getLength();

  rowCount = rowCount + 1; 

  var vProperties = {};

  vProperties.SapCostUnitId = rowCount;

  alert("row id " + rowCount);

  oModel.createEntry("/CostPerUnits", vProperties);

Accepted Solutions (1)

Accepted Solutions (1)

SergioG_TX
Active Contributor
0 Kudos

Saqib,

I doubt you can accomplish it from the odata model point of view. as you mentioned, this is easily done on the JSON model as the JSON model is a client side model, however, the odata model is a server side model.

is there a reason why you could not accomplish this via the json model ?

hope this helps

Former Member
0 Kudos

Thanks for the quick reply Sergio,

is their an easy way to create json model from the odata model? I am getting the data from odata service and have no clue how to create json model from it.

SergioG_TX
Active Contributor
Former Member
0 Kudos

Thanks, but I have a odata service running in the backend  and don't get the data as json. Can I still consume my odata service (https://iccxe61e91fa.neo.ondemand.com/indirect_cost_calculator/odata/CostPerUnits) as json?

SergioG_TX
Active Contributor
0 Kudos

yes you can still get it as JSON ... add the $format=json at the end of the service url

yourURL?$format=json

former_member182372
Active Contributor
0 Kudos

you can load data through odatamodel and copy it to json

oModel.read("/UserListSet", null, null, false, function (oEvent) {

  

var oUserModel = new sap.ui.model.json.JSONModel( {result : oEvent.results} );

sap.ui.getCore().setModel(oUserModel, "UserInfo");

...

Former Member
0 Kudos

Many thanks Sergio and Maksim,

I have create the json as recommended and can add new/empty rows to the table.

Now I need a way to write the changed data from json back to the database. Is there a way to create odatamodel from json?

karthikarjun
Active Contributor
0 Kudos

Hi Saqib,

Step1:

var getModelData = sap.ui.getCore().getModel("JSONTableModel").getData();

Step2:

var oDataCreateEntry = new sap.ui.model.odata.ODATAModel("ServiceName");

oDataCreateEntry.setHeaders({

  "X-Requested-With": "XMLHttpRequest",

  "Content-Type": "application/atom+xml;type=entry; charset=utf-8",

  //"X-CSRF-Token" : "Fetch",

  "DataServiceVersion": "2.0",

  "method": "POST",

  });

oDataCreateEntry.create("/CollectionName", getModelData,null,function(){

alert("Successfully created"

});



Thanks,

Karthik A



agentry_src
Active Contributor
0 Kudos

New question, new Discussion please.

Please mark this Discussion with a Correct Answer (closes, but does not lock the Discussion) and Helpful Answer where appropriate. See http://scn.sap.com/community/support/blog/2013/04/03/how-to-close-a-discussion-and-why   Even if you discovered the solution without any outside contributions, it helps others to understand what the solution turned out to be. 

Do not use Assumed Answered as it confuses anyone looking for the specific answer.  If you dig into the Getting Started link (top right of each SCN page), you are only instructed to mark Helpful or Correct Answers to Discussion responses. 

Thanks, Mike (Moderator)

SAP Technology RIG

Answers (1)

Answers (1)

former_member182372
Active Contributor
0 Kudos

hmm...

it adds it to

this.oData[sKey] = oEntity;

try to call

oModel.refresh() after you crete and entry to notify bindings?