如何将项目添加到 JSONModel

How to add an item to JSONModel

我需要向按钮 press 处理程序中的 JSONModel 添加一个对象。 JSONModel 在应用程序描述符 (manifest.json) 中初始化如下:

"models": {
  "invoice": {
    "type": "sap.ui.model.json.JSONModel",
    "uri": "model/Invoices.json"
  }
}
this.getOwnerComponent().getModel("invoice").data.push({/*...*/});
var oModelInvoice = this.getOwnerComponent().getModel("invoice");
var obj = {
 "ProductName": "test add",
 "Quantity": 21,
 "ExtendedPrice": 87.2000,
 "ShipperName": "Fun Inc.",
 "WeightUnit" : "G",
 "ShippedDate": "2015-04-01T00:00:00",
 "Status": "A" 
}:

oModelInvoice.setProperty("/myObject", obj);

如果模型在您的清单中已经可用并且属性已经存在,您可以像这样添加项目:

在您的控制器中点击事件:

var newItem = {
  "ProductName": "test add",
  "Quantity": 21,
  "ExtendedPrice": 87.2000,
  "ShipperName": "Fun Inc.",
  "ShippedDate": "2015-04-01T00:00:00",
  "Status": "A"
};
var oModel = this.getOwnerComponent().getModel("invoice");
oModel.setProperty("/Invoices", oModel.getProperty("/Invoices").concat(newItem));

datasourcesmodels 下的清单中:

"dataSources": {
  "invoice": {
    "uri": "./model/Invoices.json",
  }
}
....
"models": {
  "invoice": {
    "type": "sap.ui.model.json.JSONModel",
    "dataSource": "invoice"
  }
}

我找到了我们应该使用推送的答案 变种对象= {};

         obj.ProductName = "test add me";
          obj.Quantity = 120;
          obj.Status = "C";

         var oModel = this.getModel("invoice");
         var localdata = oModel.getData();
         localdata.Invoices.push(obj);
         oModel.setData(localdata);