I am trying to add new lines on a SAPUI5 table by a click of a button. I have seen plenty of online tutorials but I have not found exactly my use case.
The JSON is loaded through a mock server for now (for testing purposes and has this structure:
{
"Invoices": [
{
"ProductName": "Pineapple",
"Quantity": 21,
"ExtendedPrice": 87.2000,
"ShipperName": "Fun Inc.",
"ShippedDate": "2015-04-01T00:00:00",
"Status": "A"
},
...
]
}
I have a table that is defined like this in the view:
xmlns:html="http://www.w3.org/1999/xhtml" displayBlock="true">
number="{ parts: [{path: 'invoice>ExtendedPrice'}, {path: 'view>/currency'}], type: 'sap.ui.model.type.Currency', formatOptions: { showMeasure: false } }"
unit="{view>/currency}" state="{= ${invoice>ExtendedPrice} > 50 ? 'Error' : 'Success' }"/>
and the controller contains this function (I am only pasting the part that works):
addRow: function() {
var oList = this.getView().byId("ins");
var oDt = oList.getBinding("items").getModel().oData;
}
However, I am not sure how to proceed. I have tried push() in the model, but I am getting errors. What I was thinking of doing now is to add an empty row in oDt and bind it to the table, but I am not sure how to proceed with this, and whether it is the optimal solution.
EDIT: How to Add a New ColumnListItem to a Table was marked as potential duplicate. This question is on the same topic, but the approach of the OP does not seem to match my use case (I am new to SAPUI5, so I might be wrong - in which case, please forgive me).
解决方案
this.getView().getModel('invoice').getProperty('/Invoices').push({/* new invoice data */})
this.getView().getModel('invoice').create('/Invoices', {/* new invoice data */})
or
this.getView().getModel('invoice').createEntry('/Invoices', {/* new invoice data */})
this.getView().getModel('invoice').submitChanges();