如何将项目添加到JSONModel

时间:2019-04-29 14:17:18

标签: sapui5

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

"models": {
  "invoice": {
    "type": "sap.ui.model.json.JSONModel",
    "uri": "model/Invoices.json"
  }
}
this.getOwnerComponent().getModel("invoice").data.push({/*...*/});

3 个答案:

答案 0 :(得分:1)

如果清单中已有模型并且属性已经存在,则可以按以下方式重置属性:

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

var data = {
  "ProductName": "test add",
  "Quantity": 21,
  "ExtendedPrice": 87.2000,
  "ShipperName": "Fun Inc.",
  "WeightUnit": "G",
  "ShippedDate": "2015-04-01T00:00:00",
  "Status": "A"
};

var oModel = this.getModel("invoice");
oModel.setProperty("/ProductName", "test add");
oModel.setProperty("/Quantity", 21);

在清单中datasourcesmodels下:

"dataSources": {
  "invoice": {
    "uri": "./model/Invoices.json", //if available
    "type": "OData",
    "settings": {
      "odataVersion": "2.0",
      "localUri": "",
      "annotations": []
    }
  }
}
....
"models": {
  "i18n": {
    "type": "sap.ui.model.resource.ResourceModel",
    "settings": {
      "bundleName": "com.my.app.i18n.i18n"
    }
  },
  "invoice": {
    "type": "sap.ui.model.json.JSONModel",
    "settings": {
      "defaultOperationMode": "Server",
      "defaultBindingMode": "TwoWay",
      "defaultCountMode": "None",
      "useBatch": false
    },
    "dataSource": "invoice"
  }

答案 1 :(得分:0)

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);

答案 2 :(得分:0)

我发现我们应该使用推送  var obj = {};

         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);