简体   繁体   English

在SAPUI5 / OpenUI5中更新JSON模型

[英]Updating JSON Model in SAPUI5/OpenUI5

I have one json model, model/salesOrder.json, 我有一个json模型model / salesOrder.json,

{
  "HeaderSet" : []
}

The initialization, 初始化,

var SalesOrderModel = new sap.ui.model.json.JSONModel("model/SalesOrder.json");
sap.ui.getCore().setModel(SalesOrderModel, "SOModel");

On the time of creating Sales Order , creating one object which have direct keys like FirstName , LatName ,etc. 在创建Sales Order ,创建一个具有直接键(如FirstNameLatName等)的对象。 and pushing to this HeaderSet array without any keys. 并在没有任何键的情况HeaderSet送到此HeaderSet数组。

var salesOrderModel = this.getView().getModel("SOModel");
var salesOrderData = salesOrderModel.getData();
salesOrderData.HeaderSet.push(SoObject);
salesOrderModel.setData(salesOrderData);

Suppose I have 3 objects in this model, I need to update a specific object in this model? 假设我在这个模型中有3个对象,我需要在这个模型中更新一个特定的对象吗? How can I do that? 我怎样才能做到这一点?

Master Page View, 母版页视图

<List id="listMyOrder" growing="true" growingThreshold="10" growingScrollToLoad="true" noDataText="No drafts found" mode="SingleSelectMaster" items="{path : 'SOModel>/HeaderSet' }" select="handleMyOrderSelect">
       <ObjectListItem title="{SOModel>PurchaseOrderNumber}">
            <attributes>
                   <ObjectAttribute text="Due Date : {path : 'SOModel>PurchaseOrderDate' }" />
           </attributes>
     </ObjectListItem>
</List>

Master Page Controller, 母版页控制器,

handleMyOrderSelect: function (evt) {
    this._showDetail(evt.getParameter("listItem"));
},

_showDetail: function (item) {
    _salesOrderIDForCart = item.getBindingContext("SOModel").getObject().SalesOrderID
    sap.ui.getCore().getEventBus().publish("nav", "to", {
        id: "SoDetail",
        data: {
            source: "MyOrders",
            SalesOrderID: item.getBindingContext("SOModel").getObject().SalesOrderID,
            context: item.getBindingContext("SOModel")
        }
    });
}

Now, I can directly bind other details in detail page using this context, 现在,我可以使用此上下文直接在详细信息页面中绑定其他详细信息,

<List showSeparators="None" id="tblSummary">
  <InputListItem label="PO Number">
    <Label text="{SOModel>PurchaseOrderNumber}" design="Bold"/>
  </InputListItem>
  <InputListItem label="PO Date">
    <Label text="{path : 'SOModel>PurchaseOrderDate'}" design="Bold"/>
  </InputListItem>
</List>

In this detail page I have a button which will read this context and populate in an editable form. 在此详细信息页面中,我有一个按钮,它将读取此上下文并以可编辑的形式填充。 And here I need to update this specific edited item in the correct object of the model. 在这里,我需要在模型的正确对象中更新此特定的已编辑项目。 I tried by adding a unique dynamic key to each object, but at that time my binding will not work. 我尝试通过向每个对象添加唯一的动态键来进行操作,但是那时我的绑定无法正常工作。

Almost same thread here http://scn.sap.com/thread/3464458 and http://scn.sap.com/thread/3386927 这里的线程几乎相同: http://scn.sap.com/thread/3464458http://scn.sap.com/thread/3386927

You have a client model (JSON) and want to replace an element of the HeaderSet array with an updated version from the object that comes from your editable form. 您有一个客户端模型(JSON),并且想要用来自可编辑表单的对象的更新版本替换HeaderSet数组的元素。 We're assuming here that you're not wanting to directly have two-way binding back to the JSON model, because you want to do input validation first, for example. 我们在这里假设您不想直接进行双向绑定回到JSON模型,因为例如您想先进行输入验证。

You have a unique key SalesOrderID that we can see you already using when you publish onto the event bus. 您具有唯一键SalesOrderID,当您发布到事件总线时,我们可以看到您已经在使用它。 Let's assume this is stored in currentSalesOrderID and use this to find the right object to replace in the array with the new data, in an object we'll call updatedOrder: 假设它存储在currentSalesOrderID中,并使用它来找到要用新数据替换数组中的正确对象,在该对象中,我们将其称为updatedOrder:

// Get the data from the model
var salesOrderData = salesOrderModel.getData();

// Find the index of the object via the SalesOrderID
var index = salesOrderData.HeaderSet
    .map(function(order) { return order.SalesOrderID; })
    .indexOf(currentSalesOrderID);

// Replace the order in the array
salesOrderData.HeaderSet.splice(index, 1, updatedOrder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM