Search code examples
data-bindingsapui5

What is the proper way to add records to a V2 odata model with two way binding in SAP UI5?


I have a SAP UI5 V2 odata model with a two way binding to a table and to a form.

The table is displaying all records, and when clicking on a record, I am using setBindingContext to bind the selected table record to the form for editing. When typing in the form, the values dynamically update in the table (because of the two way binding). model.submitChanges() writes the change back to the server.

So displaying the list, and editing records in the list are working just fine.

Now for the problem How to create records with the same form?

I also want to use the same form for adding new records, but I cannot figure out how to unbind the form from a previously selected record, or otherwise create a new blank entry in the data model to be sent to the server.

All of the tutorials I have been able to find on doing UI5 Odata CRUD operations don't really address this problem.


Solution

  • I discovered this example in the documentation which pointed me in the correct direction.

    In summary, you use the oModel.createEntry method to create a new entry in the oData Model. You then have to bind this new entry to your form with setBindingContext - this is the part I was missing.